繁体   English   中英

如何获取 HTML 元素的变量属性?

[英]How to get a variable attribute of an HTML element?

我有一个 ng-repeat,我在其中设置了一个属性,如下所示:

<div ng-repeat="item in itemsList"
     stepType="{{item.stepType}}">
     {{item.itemValue}}
</div>

item.stepType 可以有值 'task' 或 'action' 我试图像这样获取属性 'stepType':

let stepType = el.getAttributeNode("stepType");
console.log(stepType.value);

我得到的结果是:

{{item.stepType}}

但预期的结果是taskaction

我也尝试使用函数getAttribute()获取属性,但结果是一样的。

我还注意到,在上面的代码中,如果我记录stepType的值而不是stepType.value ,我得到的对象看起来像这样stepType: 'task'

我怎样才能得到期望值?

如果属性名称已规范化

<div ng-repeat="item in itemsList"
     step-type="{{item.stepType}}">
     {{item.itemValue}}
</div>

自定义 AngularJS 指令可以$observe属性:

app.directive("stepType", function() {
    return {
        link: postLink
    };
    function postLink(scope, elem, attrs) {
        attrs.$observe("stepType", function(value) {
            console.log(value);
        });
    }
})

控制台将记录对属性内插值的更改。

尝试删除双引号。 替换这个

<div ng-repeat="item in itemsList"
     stepType="{{item.stepType}}">
     {{item.itemValue}}
</div>

<div ng-repeat="item in itemsList"
     stepType={{item.stepType}}>
     {{item.itemValue}}
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM