2016-08-17 38 views
1

我有以下问题,我使用ng-repeat生成了几个表格,我使用ng-model来捕获textarea值,它完美地工作,但是当我尝试在由ng生成的表单中的textarea元素重复文本同时在所有textarea元素内被复制。取消ng-repeats内的ng-models

enter image description here

是有一些方法来解除绑定的NG-型号?

<div ng-repeat="PostList in PostList"> 
    <form id="PostFormComments" role="form"> 
     <div class="row input-field"> 
     <textarea data-ng-model="data.newComment" placeholder="write something?...." type=text></textarea> 
     </div> 
     <a name="action" ng-disabled="!data.newComment" data-ng-click="addCommentPost();" ng-class="clear" class="secondary-content btn-floating waves-effect waves-light light-blue darken-2"></a> 
    </form> 
    </div> 

回答

1

你可以做的是使用$index它给你当前ng-repeat的索引,通过创建一个对象,并访问它。

所以,你会做这样的事情:$scope.data = {}

,然后你不得不喜欢的形式:

<div ng-repeat="PostList in PostList"> 
    <form id="PostFormComments" role="form"> 
     <div class="row input-field"> 
     <textarea data-ng-model="data[$index].newComment" placeholder="write something?...." type=text></textarea> 
     </div> 
     <a name="action" ng-disabled="!data[$index].newComment" data-ng-click="addCommentPost();" ng-class="clear" class="secondary-content btn-floating waves-effect waves-light light-blue darken-2"></a> 
    </form> 
    </div> 

,你会看到不同的textarea ng-models$index作为管辖。

+0

就是这样!谢谢! data-ng-click =“addCommentPost($ index);” JS:$ scope.data = {}; $ scope.addCommentPost = function(index){ var comment = $ scope.data [index] .newComment;的console.log(评语); } –