我正在尝试在students
数组中添加值,但下面的代码无效。
js
代码是 -
angular.module('formExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.students=[
'Scarlett Johansson','Jennifer Lawrence','Emma Stone','Kristen Stewart'
];
$scope.add = function(name){
$scope.students.push(name);
};
}]);
html
代码是 -
<body ng-app="formExample">
<div ng-controller="ExampleController">
<p ng-repeat="stud in students">
{{stud}}
</p>
</div>
<input type='text' ng-model="name"/>
<button ng-click="add(name);">add</button>
</body>
答案 0 :(得分:2)
您的button
和input
未包含在div
ng-controller="ExampleController"
范围内。
<body ng-app="formExample">
<div ng-controller="ExampleController">
<p ng-repeat="stud in students">
{{stud}}
</p>
<input type='text' ng-model="name"/>
<button ng-click="add(name);">add</button>
</div> <!-- close div here -->
</body>
答案 1 :(得分:0)
您需要推入$scope.students
数组,因为范围中没有定义students
数组:
$scope.add = function(name) {
$scope.students.push(name);
};
答案 2 :(得分:0)
$scope.add = function(name) {
$scope.students.push(name);//
};