Angular: 通过 ng-attr-id 获取 ng-model?
Angular: Get ng-model by ng-attr-id?
有没有办法通过 ng-attr-id
获得 ng-model
值?
我正在制作一个Comment/Reply框,我想获取当前回复的值。
这是我的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>
您不需要通过选择器检索元素值。单击时在 sendReply
函数本身内传递 replytxt
值。
ng-click="sendReply(comment.id, replytxt)"
$scope.sendReply = function(commentId, replytxt){
建议:与其让 replytxt
像 ng-model
那样独立存在,不如把它放在评论级别 属性 上,比如 [=17] =],这样您就不需要负责将 replytxt
值单独传递给服务器。
ng-click="sendReply(comment)"
代码
$scope.sendReply = function(comment){
console.log(comment.id, comment.replytxt);
}
有没有办法通过 ng-attr-id
获得 ng-model
值?
我正在制作一个Comment/Reply框,我想获取当前回复的值。
这是我的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>
您不需要通过选择器检索元素值。单击时在 sendReply
函数本身内传递 replytxt
值。
ng-click="sendReply(comment.id, replytxt)"
$scope.sendReply = function(commentId, replytxt){
建议:与其让 replytxt
像 ng-model
那样独立存在,不如把它放在评论级别 属性 上,比如 [=17] =],这样您就不需要负责将 replytxt
值单独传递给服务器。
ng-click="sendReply(comment)"
代码
$scope.sendReply = function(comment){
console.log(comment.id, comment.replytxt);
}