错误:[ng:areq] Argument' simpleController'不是一个功能,未定义

时间:2015-01-15 09:30:53

标签: javascript angularjs

我在这里做错了什么,我是棱角分明的新手,它显示上面的错误,这是我的代码

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.js"></script>
        <script>
            function simpleController($scope){
                $scope.customers=[
                    {name:'Alphy Poxy',city:'Mbita'},
                    {name:'Kibaki Watson',city:'Kikuyu'},
                    {name:'John Legend',city:'Lake'}, 
                    {name:'Sony',city:'HB'}
                ];
            }
        </script>

    </head>
    <body>
        <div class="container-fluid" ng-controller="simpleController">
            Name: <input type="text" ng-model="name"/>
            <ul>
                <li ng-repeat="coders in customers | filter:name | orderBy:'name'">{{coders.name}}-{{coders.city}}</li>
            </ul>
        </div>
    </body>   

3 个答案:

答案 0 :(得分:7)

您尚未正确创建控制器。请参阅以下代码段。

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

myApp.controller('simpleController', ['$scope', function($scope) {
    $scope.customers=[
                    {name:'Alphy Poxy',city:'Mbita'},
                    {name:'Kibaki Watson',city:'Kikuyu'},
                    {name:'John Legend',city:'Lake'}, 
                    {name:'Sony',city:'HB'}
                ];    
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container-fluid" ng-app="myApp" ng-controller="simpleController">
    Name: <input type="text" ng-model="name"/>
    <ul>
        <li ng-repeat="coders in customers | filter:name | orderBy:'name'">{{coders.name}}-{{coders.city}}</li>
    </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

或者至少你需要在父html标签中发布ng-app指令,请参阅下面的演示

 function simpleController($scope) {
   $scope.customers = [{
     name: 'Alphy Poxy',
     city: 'Mbita'
   }, {
     name: 'Kibaki Watson',
     city: 'Kikuyu'
   }, {
     name: 'John Legend',
     city: 'Lake'
   }, {
     name: 'Sony',
     city: 'HB'
   }];
 }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <div class="container-fluid" ng-controller="simpleController">


    Name:
    <input type="text" ng-model="name" />
    <ul>
      <li ng-repeat="coders in customers | filter:name | orderBy:'name'">{{coders.name}}-{{coders.city}}</li>
    </ul>
  </div>
</div>

答案 2 :(得分:0)

正如@Christos所说的那样,最好的办法是真正做到。

或者您可以添加:

 1. add np-app along with a module name  ( ng-app="app" )
 2. add controller assigning it in.  (  app.controller('SimpleController', SimpleController);
    <script>
        function simpleController($scope){
            $scope.customers=[
                {name:'Alphy Poxy',city:'Mbita'},
                {name:'Kibaki Watson',city:'Kikuyu'},
                {name:'John Legend',city:'Lake'}, 
                {name:'Sony',city:'HB'}
            ];
        }
        app.controller('SimpleController', SimpleController);
    </script>

</head>
<body>
    <div class="container-fluid" ng-controller="simpleController" ng-app="app">
        Name: <input type="text" ng-model="name"/>
        <ul>
            <li ng-repeat="coders in customers | filter:name | orderBy:'name'">{{coders.name}}-{{coders.city}}</li>
        </ul>
    </div>
</body>