我在这个问题{@ 3}}中首先回答@Gloopy AngularJS ng-model $scope in ng-repeat is undefined
我的问题是,我是否可以从 ng-repeat scope 之外更新 ng-repeat范围中的 ng-model 值,如下所示:< / p>
<div ng-app="MyApp">
<div ng-controller="PostCtrl">
<div ng-repeat="post in posts">
<strong>{{post}}</strong>
<input type="text" ng-model="postText">
</div>
/*Update ng-model values (in ng-repeat scope) using single click*/
<a href="#" ng-click="savePost(postText)">save post</a>
</div>
</div>
这是控制器:
angular.module('MyApp',[]);
function PostCtrl($scope) {
$scope.posts = ['tech', 'news', 'sports', 'health'];
$scope.savePost = function(post){
alert( 'post stuff in textbox: ' + post);
}
}
我试过但未定义:(我的实验小提琴在这里http://jsfiddle.net/VnQqH/128/如果有人有这个问题的解决方案请分享。非常感谢提前。
答案 0 :(得分:1)
您可以使用$ parent来传递ng-repeat指令之外的范围变量,如下所示
HTML:
<div ng-app="MyApp">
<div ng-controller="PostCtrl">
<div ng-repeat="post in posts">
<strong>{{post}}</strong>
<input type="text" ng-model="$parent.postText">
</div>
<!--Update on single click-->
<a href="#" ng-click="savePost(postText)">save post</a>
</div>
</div>
控制器:没有改变任何东西,保留你拥有的东西