javascript - AngularJs Injector Module Error - Stack Overflow

i'm new in AngularJs (started an couple hours ago) and i'm trying to plete the AngularJs in 6

i'm new in AngularJs (started an couple hours ago) and i'm trying to plete the AngularJs in 60 minutes'ish tutorial. But i got stucked on the first controllers example. This is my code:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Angular Js</title>
    <meta charset="iso-8859-1">

</head>
<body>

    <div class="container" data-ng-controller="CustomerController">
        <input type="text" ng-model="name"> {{name}}

        <ul>
            <li data-ng-repeat="cust in customers">
                {{ cust.name}} - {{cust.city}}
            </li>
        </ul>
    </div>

    <script src=".3.0-beta.17/angular.js"></script>

    <script>
        function CustomerController($scope) 
        {   
            $scope.customers = [
                {name:'Leonardo', city:'Phoenix'}, 
                {name:'Michelangelo', city:'Los Angeles'},
                {name:'Rafael', city:'New York'}, 
                {name:'Donatello', city:'Texas City'}
            ];
        }
    </script>
</body>
</html>

This is the error i got from the ChromeDev Console:

Uncaught 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 th...<omitted>...2) angular.js:78
(anonymous function) angular.js:78
(anonymous function) angular.js:4003
forEach angular.js:328
loadModules angular.js:3964
createInjector angular.js:3904
doBootstrap angular.js:1500
bootstrap angular.js:1515
angularInit angular.js:1427
(anonymous function) angular.js:23362
trigger angular.js:2653
(anonymous function) angular.js:2933
forEach angular.js:328
eventHandler angular.js:2932

As a newbie, I dont want to get into injector or modules and stuff. Since the tutorial is get to work only with this piece of code.

Thanks!

i'm new in AngularJs (started an couple hours ago) and i'm trying to plete the AngularJs in 60 minutes'ish tutorial. But i got stucked on the first controllers example. This is my code:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Angular Js</title>
    <meta charset="iso-8859-1">

</head>
<body>

    <div class="container" data-ng-controller="CustomerController">
        <input type="text" ng-model="name"> {{name}}

        <ul>
            <li data-ng-repeat="cust in customers">
                {{ cust.name}} - {{cust.city}}
            </li>
        </ul>
    </div>

    <script src="https://ajax.googleapis./ajax/libs/angularjs/1.3.0-beta.17/angular.js"></script>

    <script>
        function CustomerController($scope) 
        {   
            $scope.customers = [
                {name:'Leonardo', city:'Phoenix'}, 
                {name:'Michelangelo', city:'Los Angeles'},
                {name:'Rafael', city:'New York'}, 
                {name:'Donatello', city:'Texas City'}
            ];
        }
    </script>
</body>
</html>

This is the error i got from the ChromeDev Console:

Uncaught 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 th...<omitted>...2) angular.js:78
(anonymous function) angular.js:78
(anonymous function) angular.js:4003
forEach angular.js:328
loadModules angular.js:3964
createInjector angular.js:3904
doBootstrap angular.js:1500
bootstrap angular.js:1515
angularInit angular.js:1427
(anonymous function) angular.js:23362
trigger angular.js:2653
(anonymous function) angular.js:2933
forEach angular.js:328
eventHandler angular.js:2932

As a newbie, I dont want to get into injector or modules and stuff. Since the tutorial is get to work only with this piece of code.

Thanks!

Share Improve this question asked Aug 5, 2014 at 22:48 Italo AyresItalo Ayres 2,6632 gold badges18 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

first you defined ng-app="myApp" but you didn't define any module named "myApp", so just use ng-app.

second, seems like the angular version you used 1.3.0-beta.17 doesn't work if you don't declare a module like this

angular.module("myApp", [])

if you want to to make the example you posted work, use a different angular version, like 1.2.5

here's a working jsbin of your code

I think you should always declare at least one module.

Try:

<script>

    var myApp = angular.module('myApp',[]);

    function CustomerController($scope) 
    {   
        $scope.customers = [
            {name:'Leonardo', city:'Phoenix'}, 
            {name:'Michelangelo', city:'Los Angeles'},
            {name:'Rafael', city:'New York'}, 
            {name:'Donatello', city:'Texas City'}
        ];
    }
</script>

There are 2 solutions:

First, as your error says: "Module 'myApp' is not available!". You need first to define the module myApp, so that angular could instantiate it. Then add your controller's constructor function to myApp module using the .controller() method.

var myApp = angular.module('myApp', []);

myApp.controller('CustomerController', ['$scope',
    function($scope) {
        $scope.customers = [{
            name: 'Leonardo',
            city: 'Phoenix'
        }, {
            name: 'Michelangelo',
            city: 'Los Angeles'
        }, {
            name: 'Rafael',
            city: 'New York'
        }, {
            name: 'Donatello',
            city: 'Texas City'
        }];
    }
]);

DEMO

Second, just remove myApp from your <html> tag and everything will work just fine

<html ng-app="">

DEMO

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744291077a4567035.html

相关推荐

  • javascript - AngularJs Injector Module Error - Stack Overflow

    i'm new in AngularJs (started an couple hours ago) and i'm trying to plete the AngularJs in 6

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信