1

ng-model 값을 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. 클릭시 sendReply 내부에 replytxt 값이 전달됩니다.

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 다행을 알고, 감사합니다;) –