1

の値をng-attr-idで取得する方法はありますか?角度:ng-attr-idでng-modelを取得しますか?

私はコメント/返信ボックスを作成しています。私は現在の返信の価値を得たいと思います。ここで

は、ここに私のhtml--

<div class="comments-list" ng-show="CommentsLoaded"> 
    <ul> 
     <li ng-repeat="comment in comments"> 
      <div> 
       {{comment.content}} 
      </div> 
      <div ng-click="showReply(comment.id)"> 
       Reply 
      </div> 
      <div ng-class="hideReply" ng-attr-id="{{'reply-'+comment.id}}"> 
       <textarea ng-model="replytxt" ng-attr-id="{{'replytxt-'comment.id}}"></textarea> 
       <div class="form-group"> 
        <button type="button" ng-click="sendReply(comment.id)"> 
         Publier 
        </button> 
       </div> 
      </div> 
     </li> 
    </ul> 
</div> 

あり、これはコンソールで上記の機能のショーangularjs--

$scope.sendReply = function(commentId){ 
    var elm = document.getElementById('replytxt-'+commentId); 
    console.log(elm); 
} 

です:あなたは必要ありません

<textarea ng-model="replytxt" ng-attr-id="{{'replytxt-'+comment.id}}" class="ng-pristine ng-valid ng-touched" id="replytxt-31"></textarea> 

答えて

2

そのセレクタによって要素値をretieveする。クリックするとreplytxtの値がsendReplyの内部に渡されます。

ng-click="sendReply(comment.id, replytxt)" 

$scope.sendReply = function(commentId, replytxt){ 

提案むしろng-modelとして独立して存在しreplytxtを持つよりも、サーバーに個別にreplytxt値を渡すの世話をする必要がないように、あなたは、comment.replytxtのようなコメントレベルのプロパティの上に置くことができます。

ng-click="sendReply(comment)" 

コード

$scope.sendReply = function(comment){ 
    console.log(comment.id, comment.replytxt); 
} 
+0

は素晴らしい作品。あなたの提案は私のものよりも優れています。ありがとうございました –

+1

@AmadouBeyeそれを知ってうれしい、ありがと;) –

関連する問題