2014-11-23 49 views
0

學習使用angularJS,角度JS範圍變量不起作用

我在我的index.html中有這個特定的代碼。動態打字在上半部分起作用,但注射不起作用。什麼可能會出錯?

代碼:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Test1</title> 
    </head> 
    <body ng-app=""> 
<!-- Dynamic Type --> 

     <input type="text" ng-model="name1" /><br> 
     <h1>Hello {{name1}}</h1> 

<!-- End dynamic Type --> 

<!-- Scope Injection begin--> 
     <div class="container" ng-controller="myController"> 
     <input type="text" ng-model="naam" /><br> 
     <h3>Looping</h3> 
     <ul> 
      <li data-ng-repeat="cust in customerlist | filter:naam | orderBy:'city'"> {{cust.name | uppercase}} - {{cust.city}} </li> 
     </ul> 
     </div> 
     <script type="text/javascript" src="js/angular.min.js"></script> 
     <script type="text/javascript"> 
     function myController ($scope) { 

      $scope.customerlist = [ 
      {name: 'Raj Bannerjee', city: 'Zaire'}, 
      {name: 'Prasun Bannerjee', city: 'Udaipur'}, 
      {name: 'Raj Malhotra', city: 'Dubai'}, 
      {name: 'Prasun Joshi', city: 'Mumbai'} 
      ]; 

     } 

     </script> 
    </body> 
</html> 
+2

你怎麼看它不工作?這是一個jsfiddle(略有修改),工作正常。 http://jsfiddle.net/b69p4zj9/ – deitch 2014-11-23 08:44:20

+0

也許我看到它。你問過濾器是如何工作的? – deitch 2014-11-23 08:46:49

+0

你需要告訴角度myController是一個控制器'angular.controller('myController',function($ scope){....' – 2014-11-23 09:08:23

回答

3

從版本1.3角殘疾人使用全局控制器構造函數:

https://github.com/angular/angular.js/commit/3f2232b5a181512fac23775b1df4a6ebda67d018

With the exception of simple demos, it is not helpful to use globals for controller constructors. This adds a new method to $controllerProvider to re-enable the old behavior, but disables this feature by default.

BREAKING CHANGE: $controller will no longer look for controllers on window . The old behavior of looking on window for controllers was originally intended for use in examples, demos, and toy apps. We found that allowing global controller functions encouraged poor practices, so we resolved to disable this behavior by default.

所以,你必須重構它就像這樣:

angular.module('app',[]) 

.controller('myController', ['$scope', function($scope) { 
    $scope.customerlist = [ 
    {name: 'Raj Bannerjee', city: 'Zaire'}, 
    {name: 'Prasun Bannerjee', city: 'Udaipur'}, 
    {name: 'Raj Malhotra', city: 'Dubai'}, 
    {name: 'Prasun Joshi', city: 'Mumbai'} 
    ]; 
}]); 

而且還指你的模塊:

<body ng-app="app"> 
+0

完美工作。非常感謝 – 2014-11-23 09:51:08