当控制器数组传入模块时,Angular JS不起作用?

时间:2014-03-24 06:36:29

标签: javascript html angularjs

在下面的代码中,未注释的部分不起作用(“第三种方法”)。

所有页面都不起作用。不是它的一部分..

但是,当我使用“第二种方法”时,它正在运作..

<html data-ng-app="demoApp">
   <head>
        <title>...</title>
   </head>
   <body>
      <div class="container" data-ng-controller="SimpleController"> 
         <h3>Using controller data</h3>
         Name: <input type="text" data-ng-model="personTxt"/> {{personTxt}}
         <ul>
           <li data-ng-repeat="person in customers | filter:personTxt | orderBy:'city'">{{person.name}} . {{person.city}}</li>
         </ul>
      </div>

      <script src="angular.min.js"> //Should be at the starting of the script</script>
      <script>
         var demoApp = angular.module('demoApp', []);       
         /* Third method */     
         var controllers = {};      
         controllers.SimpleController =
         function ($scope) {
            $scope.customers = [
               {name:'Sahan Chamika Munasinghe', city:'Kaburupitiya'},
               {name:'Nuwan Sachinthana', city:'Walgama'},
               {name:'Diunuge Buddhika Wijesinghe', city:'Matara'}
            ];

            $scope.personTxt = "My Name";
         });

         demoApp.controller(controllers);
       </script>
   </body>
</html>

第二种

/* Second method */
/*
   demoApp.controller('SimpleController', 
   function ($scope) {
      $scope.customers = [
         {name:'Sahan Chamika Munasinghe', city:'Kaburupitiya'},
         {name:'Nuwan Sachinthana', city:'Walgama'},
         {name:'Diunuge Buddhika Wijesinghe', city:'Matara'}
      ];            
      $scope.personTxt = "My Name";
   });

   demoApp.controller('SimpleController', SimpleController);
*/

1 个答案:

答案 0 :(得分:0)

在定义的最后一行有一个额外的尾随关闭:

controllers.SimpleController = 
                function ($scope)
                {
                    $scope.customers = [
                        {name:'Sahan Chamika Munasinghe', city:'Kaburupitiya'},
                        {name:'Nuwan Sachinthana', city:'Walgama'},
                        {name:'Diunuge Buddhika Wijesinghe', city:'Matara'}
                    ];

                    $scope.personTxt = "My Name";
                });
分号前的

摆脱最后一个),一切都应该有效