Angular:通过ng-attr-id获取ng-model?

时间:2017-01-16 04:12:27

标签: javascript angularjs angularjs-directive angular-ngmodel

有没有办法通过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>

1 个答案:

答案 0 :(得分:2)

您不需要通过其选择器检索元素值。点击后,在replytxt函数内部传递sendReply值。

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

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

建议:您可以将replytxt作为ng-model独立的评论级别属性,而不是comment.replytxt,这样您就不会{39} ; t需要注意将replytxt值单独传递给服务器。

ng-click="sendReply(comment)"

<强>代码

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