繁体   English   中英

Angular JS:控制器不起作用。 当我尝试使用ng-init时,所有事情都完美运行

[英]Angular JS: Controller not working. all things work perfect when i try with ng-init

我是AngularJS的新手。 我无法让控制器工作。 但是, ng-init可以完美工作。 这是代码:

<div data-ng-controller="SimpleController">
  <input type="text" data-ng-model="yourName" placeholder="Enter a name here">
  <h3>
    <ul> 
      <li data-ng-repeat="cust in customers | filter:yourName | orderBy : 'name'"> {{ cust.name | uppercase }} - {{ cust.city | uppercase }} </li>
    </ul>
  </h3>
</div>
<script src="js/angular.min.js"></script>
<script>
  function SimpleController($scope) {
    $scope.customers = [{
      name: 'rutunj',
      city: 'surat'
    }, {
      name: 'aushik',
      city: 'Jugal'
    }, {
      name: 'kushik',
      city: 'Lugal'
    }];
  }
</script>

在脚本中,您已完成此操作

function SimpleController($scope) {
  $scope.customers = [{
    name: 'rutunj',
    city: 'surat'
  }, {
    name: 'aushik',
    city: 'Jugal'
  }, {
    name: 'kushik',
    city: 'Lugal'
  }];
}

但是您尚未定义角度模块,因此请在脚本内按如下所示更改代码。

angular.module('myApp', []).controller('SimpleController', SimpleController);

function SimpleController($scope)
  // Controller code
}

并在div中添加ng-app

<div ng-app="myApp" data-ng-controller="SimpleController">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM