Javascript AngularJS-无法在我的方法中读取变量的更改值,它返回旧值
我有这样的代码Javascript AngularJS-无法在我的方法中读取变量的更改值,它返回旧值,javascript,angularjs,Javascript,Angularjs,我有这样的代码 BackendApp.controller('CustomCtrl', function ($scope) { $scope.names = [ {"name" : "John", "Year" : 18}, {"name" : "Tony", "Year" : 19} ]; $scope.saveTemplate = function() { console.log($scope.names);
BackendApp.controller('CustomCtrl', function ($scope) {
$scope.names = [
{"name" : "John", "Year" : 18},
{"name" : "Tony", "Year" : 19}
];
$scope.saveTemplate = function() {
console.log($scope.names);
// [
// {"name" : "John", "Year" : 18},
// {"name" : "Tony", "Year" : 19}
// ];
}
});
和html
<div ng-controller="CustomCtrl">
<div ng-repeat="name in names">
<input type="text" ng-model="name.name" />
</div>
<div ng-repeat="name in names">
{{name.name}} // if we write something in input we see the changed value here
</div>
<a href="javascript:void(0)" ng-click="saveTemplate()">Save</a> // when we click on it, we get old value vor the names array
</div>
如何在saveTemplate方法中获取名称数组的访问权限?:/它只返回变量的旧值修复您的ng模型:
替换:
<div ng-repeat="name in names">
<input type="text" ng-model="name{{ names[$index].name }}" />
</div>
与:
我使用item是为了避免混淆变量是什么。很抱歉,描述错误,我编写的代码与您的代码一样,它没有帮助它没有帮助,所以到底发生了什么?在saveTemplate方法中,我得到了names数组的旧值,console.log始终返回此值[{name:John,Year:18},{姓名:托尼,年份:19}],但在视图中从input@HaykAramyan请提供一个plunker来说明您的问题。name.name看起来很奇怪…Hayk我试图复制它,但似乎它工作正常,我复制了您的代码-您的代码工作ooh,抱歉,伙计们,这是我的错,我在html中有语法错误,感谢您的时间和回复。
<div ng-repeat="item in names">
<input type="text" ng-model="item.name" />
</div>