加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – ng-attr不评估指令元素

发布时间:2020-12-17 16:58:08 所属栏目:安全 来源:网络整理
导读:我试图使用元素属性将数据从控制器传递到隔离的范围.这是我在视图中的标记: comment ng-attr-cid="{{question.id}}" ctype="questions"/div 这是指令: 'use strict'angular.module('arlo.directives').directive "comment",['Comment',(Comment) - directi
我试图使用元素属性将数据从控制器传递到隔离的范围.这是我在视图中的标记:

<comment ng-attr-cid="{{question.id}}" ctype="questions"></div>

这是指令:

'use strict'

angular.module('arlo.directives').directive "comment",['Comment',(Comment) ->
  directive =
    templateUrl: "angular/partials/comment.html"
    restrict: "E"
    scope:
      cid: "="
      ctype: "="

    link: (scope,element,attrs) ->
      scope.toggled = false
      scope.comment = null
      scope.comments

      scope.toggle = ->
        if scope.toggled is true then scope.toggled = false else scope.toggled = true
        scope.comment = null

      scope.addComment = ->
        Comment.addComment(scope.ctype,scope.cid,scope.comment).then ->
          scope.comments = Comments.commentsList
          scope.toggled = false
          scope.comment = null

      scope.loadComments = ->
        Comment.loadComments(scope.ctype,scope.cid).then ->
          scope.comments = Comments.commentsList

      scope.loadComments()
]

问题是cid被分配了“{{question.id}}”而不是question.id的值.我试图使用ng-attr-cid =“question.id”,但这也不起作用.最重要的是,ctype正在评估为未定义.

如果我在任何其他元素上添加ng-attr-cid,它会对元素进行求值并添加cid =“”.

有人可以解释我错过了什么吗?

解决方法

在隔离范围(在指令上指定范围对象时获得的范围)中,可以根据原始元素的属性将变量导入范围.

在这种情况下,不需要使用ng-attr,因为我们的指令将处理获取值.

>“=”用于复制变量,因此您只需提供变量名称,例如CID = “question.id”
>“@”用于在将变量传递给指令之前插入变量,例如CID = “{{question.id}}”.传递原始字符串也非常方便.

简而言之

>放弃ng-attr
>将指令scope.cid更改为“@”或在HTML中使用cid =“question.id”
>检查问题的价值(不确定这是否故意复数,因为你的指令中未定义ctype,这意味着问题也是未定义的.

这是一个plnkr showing the fix.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读