javascript - Angular : Get ng-model by ng-attr-id?

标签 javascript angularjs angularjs-directive angular-ngmodel

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

最佳答案

您不需要通过其选择器检索元素值。请在点击时在 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);
}

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

相关文章:

javascript - <head> 元素在 DOM 中是否始终可用,即使在 HTML 标记中不存在?

javascript - 使用过滤器对 ng-repeat 中的 $scope 对象进行排序

javascript - 需要获取Angular js ui树中新获取拖动对象信息的信息

javascript - Angular JS 和复杂指令

angularjs - 在 AngularJS 中动态加载 HTML 元素

css - angular,angular-snap.js,切换按钮点击隐藏/显示行为不应该影响标题和菜单

javascript - IOS7 最小用户界面不工作

javascript - 类中的箭头函数

JavaScript 控制台日志在 if 语句评估为 false 后自行清除

javascript - 在 AngularJS 的 session 存储中自动保存 HTML 表单