我是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模块,所以在脚本中修改如下代码:
angular.module('myApp', []).controller('SimpleController', SimpleController);
function SimpleController($scope)
// Controller code
}
并在div中添加ng-app
,如
<div ng-app="myApp" data-ng-controller="SimpleController">