gpt4 book ai didi

javascript - 如果来自外部 ng-repeat $scope 的更新也未定义,则 ng-repeat 中的 AngularJS ng-model $scope 未定义

转载 作者:行者123 更新时间:2023-12-03 12:02:17 25 4
gpt4 key购买 nike

我正在查看这个示例AngularJS ng-model $scope in ng-repeat is undefined在这个问题中,@Gloopy 首先在这里回答了 http://jsfiddle.net/VnQqH/128/

我的问题是我可以从ng-repeat范围之外更新ng-repeat范围中的ng-model值,如下所示:

<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>

这是 Controller :

angular.module('MyApp',[]);

function PostCtrl($scope) {
$scope.posts = ['tech', 'news', 'sports', 'health'];
$scope.savePost = function(post){
alert( 'post stuff in textbox: ' + post);
}
}

我尝试过,但未定义:(我的实验 fiddle 在这里http://jsfiddle.net/hot81o2z/如果有人有这个问题的解决方案,请分享。提前非常感谢。

最佳答案

您可以使用 $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>

Controller :没有改变任何东西,保留你所拥有的

Working Demo

关于javascript - 如果来自外部 ng-repeat $scope 的更新也未定义,则 ng-repeat 中的 AngularJS ng-model $scope 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com