2014-11-23 50 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