角度:通过ng-attr-id获取ng模型



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

这是角度——

$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>

不需要通过元素选择器检索元素值。单击时sendReply函数本身传递replytxt值。

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);
}

最新更新