有没有办法通过ng-model获取ng-attr-id值?

我正在评论/回复框,我想获取当前回复的值。

这是我的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>

最佳答案

您不需要通过其选择器来检索元素值。单击时,请在replytxt函数本身内部传递sendReply值。

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

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


建议:您可以将其放在replytxt之类的注释级别属性上,而不是像ng-model那样单独将comment.replytxt放在那里,这样就不必担心将replytxt值分别传递给服务器。

ng-click="sendReply(comment)"




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

关于javascript - Angular:通过ng-attr-id获取ng-model?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41669321/

10-11 08:10