Issue encountered with AngularJS - module instantiation unsuccessful

I'm currently working my way through an Angular for .Net book, and I'm stuck on a basic example that involves creating an app with two controllers. However, I keep encountering this error message and I can't figure out why the module instantiation is failing:

Unhandled exception at line 4138, column 9 in http://localhost:53990/Scripts/angular.js

0x800a139e - JavaScript runtime error: [$injector:modulerr] Failed to instantiate module myApp due to:

Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

This is the part of my code causing the issue

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <title>AngularJS with .NET</title>
    <script src="Scripts/angular.js"></script>
</head>
<body>
    <h1>Game setup</h1>
    <div ng-controller="ExampleController1">
        <h2>Player 1</h2>
        <label>Name:</label>
        <input type="text" placeholder="Please enter player 1 name" ng-model="name" ng-focus="onNameFocused()" />
        <h3 ng-show="name">Player 1 name is {{name}}</h3>
        <h3 ng-show="previousName">Previous Player 1 name was {{previousName}}</h3>
    </div>

    <div ng-controller="ExampleController2">
        <h2>Player 2</h2>
        <label>Name:</label>
        <input type="text" placeholder="Please enter player 2 name" ng-model="name" ng-focus="onNameFocused()" />
        <h3 ng-show="name">Player 2 name is {{name}}</h3>
        <h3 ng-show="previousName">Previous Player 2 name was {{previousName}}</h3>
    </div>

    <script>
        (function () {
            "use strict"
            var myAppModule = angular.module('myApp', []);
            
            myAppModule.controller('ExampleController1', ['$scope', function
                ($scope) { 
                $scope.name = "Alex Pop";
                $scope.previousName = "";
                $scope.onNameFocused = function () {
                    $scope.previousName = $scope.name;
                };
            }]);

            myAppModule.controller('ExampleController2', ['$scope', function ($scope) {
                $scope.name = "Alex Pop";
                $scope.previousName = "";
                $scope.onNameFocused = function () {
                    $scope.previousName = $scope.name;
                };
            }]);
            console.log(myAppModule.name);
        });

    </script>
</body>
</html>

Any insights on what might be going wrong here?

Answer №1

Your Angular Script was enclosed in an Immediately-Invoked Function Expression (IIFE), however, you forgot to actually call the function to load the script.

(function () {
...
});

To fix this issue, you need to either remove the function wrapper or invoke the function like this:

(function () {
...
}());

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Take out the final item from an array in AngularJS

<div class="col-md-2"> <md-button class="md-raised md-primary" ng-click="deleteDiv()">Remove</md-button> </div> scope.deleteDiv = function() { alert(scope.itemsToAdd.length); if(scope.itemsToAdd.length > 1) ...

Radio Buttons for Nested Question Toggling

My current setup involves using radio buttons and JavaScript/jQuery to handle nested questions. Based on the radio button clicked (Yes to show, No to hide), extra information is displayed or hidden. Here is an example of the HTML code: <div class="form ...

A dialog box resembling a tooltip

Are there any jQuery plugins available that mimic the functionality of the tooltip dialog in the Flow App? (See screenshot here: ). I am currently working on a web app that requires a lot of tooltip dialogs. If there isn't a plugin available, could s ...

Angular 2 Rapid Launch: Incorrect Encoding of JavaScript Files

I am brand new to learning angular 2, so I am currently attempting to get things up and running following this guide: https://angular.io/guide/quickstart The issue I am facing has left me quite puzzled. Whenever I receive any JS files as a response, they ...

What are the steps to delete data in angularjs?

I attempted to eliminate the data utilizing indexOf in AngularJS. Unfortunately, the remove function isn't functioning properly. Please guide me on identifying the mistake I may be making. JavaScript: var app = angular.module('myApp', []); ...

Best practices for removing css styles from a div element when selecting another within a Polymer application

Seeking assistance, can anyone help me? I have an iron-list with individual entries each containing a "settings" icon. When this icon is clicked, a panel slides in from the right. The issue I am facing is that I want the panel to close automatically when ...

Troubleshooting: issues with jQuery AJAX POST functionality

My goal is to perform an AJAX operation, and while the call seems to be functioning correctly, I am encountering an issue where the response I receive is just an empty string. Surprisingly, there are no errors being displayed in the console - only an empty ...

Issue with Charts.js not rendering properly during page load

My Charts.js line chart is experiencing a strange issue that I can't seem to resolve. When the page first loads, the <canvas> element appears empty: https://i.sstatic.net/HEj2P.png However, if I resize the browser window, the chart suddenly ap ...

Having an issue with Local Storage returning undefined

I've been working on a form to input values and show the data on a different page after submission. Below is my form: <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" hr ...

What is the minimum required node.js version for my project to run?

Is there a command in NPM that can display the minimum required Node version based on the project's modules? ...

Click on the appended text to remove it using Jquery

Seeking a more efficient and streamlined approach for appending and removing appended spans, divs, or text. Below is the code snippet - any insights on what could be improved? /* HTML CODE */ <div id="appendHere" style="padding:10px;border:solid 1px ...

Ensuring that each object in a list contains an optional key if any one object includes it is a task handled by Joi validation

My dataset includes various objects that must have certain keys: Date, Time, Price I am looking to include an optional key "order" for these objects. If one of them has the "order" key, then they all should. Is there a way to validate this using joi? ...

Despite receiving a 404 fetch response, the page still exists

I have encountered an issue with my Fetch code (POST) where the response shows status: 404 even though the URL returns a JSON when accessed through the browser. Surprisingly, changing the URL to https://httpbin.org/post brings back normal data. Furthermore ...

Update the radio options and link them to a particular identifier in both the database and the HTML tables

Currently, I have an AJAX request that generates a table with the following structure: | ID | Name | Radio | ======================================== | 123456 | One | Y,N | ======================================== | 78 ...

"Using the ng-click directive in AngularJS, a button radio can dynamically update the value in a

Managing two controllers can be tricky, especially when it comes to communication between them. In this scenario, we have a controller filter and a display controller. The filter controller contains a group of radio buttons, and changing the selected butto ...

The error message "Unexpected character at line 2, column 1 of JSON data" is indicating an issue with parsing the JSON data

Here is the structure of my JSON string: {"Peter":"35","Ben":"37","Joe":"43"} This PHP code demonstrates how the data is formatted: class Products{ public function example(){ $results ...

Anomaly in Form Validation with Semantic-UI

This time around, I am implementing Semantic-UI with React. In the past, I have successfully utilized Semantic-UI's form and form validation features in various projects without encountering any issues. However, a new problem has arisen, which I will ...

Sending React form data to Node Express using Axios

I'm encountering an issue that I can't quite figure out. I'm attempting to use axios to post data from React to Express. I followed a guide on connecting a React frontend to an Express server, which worked well for fetching data with a proxy ...

Update to react version 18.2.0, router-dom v6, and mui 5 for improved performance

Struggling to convert the following file or code into React's latest version and react-router-dom v6. I attempted it myself but encountered errors related to createBrowserHistory that I couldn't comprehend. My routes are stored in a JSON file and ...

Run some code and then enter interactive mode

Is there a method to run certain code (from a file or a string) before entering interactive mode in node.js? For instance, if I have script called __preamble__.js with the following content: console.log("preamble executed! poor guy!"); and a user enters ...