我必须在数字数组的第3次迭代中将客户名重命名为'Mary'。由于Angular是双向绑定,所有客户名称都更改为Mary。请找到附带的示例代码,并帮我解决这个问题?
<!DOCTYPE html>
<html data-ng-app ="myApp">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body data-ng-controller="myCtrl">
<div >
<div data-ng-repeat="cust in customer" >
<div ng-repeat="num in numbers"> {{ cust.name }} - {{ cust.city }}
<div data-ng-if="num == 4" data-ng-init="changeCustomer(cust)"></div>
</div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.customer = [{name: 'Jill', city : 'DL'},{name: 'Jack', city : 'KL'}];
$scope.numbers = [1,2,3,4,5];
$scope.changeCustomer = function(customer) {
customer.name = 'Mary';
}
});
</script>
</body>
</html>
由于
答案 0 :(得分:1)
您可以执行类似
的操作 $scope.customer = [{name: 'Jill', city : 'DL'},{name: 'Jack', city : 'KL'},....];
$scope.customer[3].name = 'Marry'
<div ng-repeat="cust in customers"> {{ cust.name }} - {{ cust.city }}</div>
或者如果需要调用changeCustomer函数,可以使用$ index跟踪并避免嵌套的ng-repeat。