javascript - AngularJS ng-model $scope in ng-repeat is undefined if update from outside ng-repeat $scope is also undefined -
i looking example angularjs ng-model $scope in ng-repeat undefined in question first answered @gloopy here http://jsfiddle.net/vnqqh/128/
my question can update ng-model values in ng-repeat scope outside of ng-repeat scope this:
<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>
here controller:
angular.module('myapp',[]); function postctrl($scope) { $scope.posts = ['tech', 'news', 'sports', 'health']; $scope.savepost = function(post){ alert( 'post stuff in textbox: ' + post); } }
i tried got undefined :( experimental fiddle here http://jsfiddle.net/hot81o2z/ if has solution problem please share. many in advance.
you can make use of $parent in order pass scope variable outside of ng-repeat directive,
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: not changed anything, kept had
Comments
Post a Comment