Javascript 检索angularjs指令中的绑定值

Javascript 检索angularjs指令中的绑定值,javascript,angularjs,Javascript,Angularjs,我知道相同的角度属性指令可以通过多种方式绑定: 数据hello-world、x-hello-world和hello:world都是绑定helloWorld属性指令的有效属性 在我的指令的link函数中,我如何知道使用了哪些属性键 因此,如果我的html是: <div data-hello-world="hi"></div><div hello:world="bye"></div> 如何知道在指令中查找哪个属性,以便记录正确的值? <div

我知道相同的角度属性指令可以通过多种方式绑定:

数据hello-world、x-hello-world和hello:world都是绑定helloWorld属性指令的有效属性

在我的指令的link函数中,我如何知道使用了哪些属性键

因此,如果我的html是:

<div data-hello-world="hi"></div><div hello:world="bye"></div>
如何知道在指令中查找哪个属性,以便记录正确的值?


<div data-hello-world="hi" one="bye">
</div>


app.directive('helloWorld', function() {
return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
        console.log(attrs.one); // bye
   console.log(attrs.helloWorld); // hi
    }
}
});
app.directive('helloWorld',function(){ 返回{ 限制:“A”, 链接:功能(范围、要素、属性){ console.log(attrs.one);//再见 console.log(attrs.helloWorld);//嗨 } } });
此处“helloWord”指令用作属性。这样,分配给“data hello world”的值即“hi”将在链接函数中定义的名为attrs的参数中找到。同时,它将查找helloWorld指令中提到的其他属性。这里我提到了“one”属性,这也可以在link函数中定义的attrs参数中找到。您可以从attrs参数获取属性值。(是的,有多种方法可以绑定一个值,正如您在描述中提到的)


任何进一步的问题,请提出您的问题并给出答案。

使用驼峰大小写命名约定(无特殊字符)从视图中访问属性和范围。看

指令有驼峰大小写的名称,如ngBind。指令可以是 通过将camel case名称转换为snake case来调用 特殊字符:、-、或。也可以选择使用该指令 前缀为x-,或数据-使其符合HTML验证程序


+1解释:)请删除最后一行。无论如何,在link函数中都没有强制要求将“helloWorld”指令名作为变量检索(因此我可以在多个指令中使用相同的link函数)。我不理解您的场景。
<div data-hello-world="hi" one="bye">
</div>


app.directive('helloWorld', function() {
return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
        console.log(attrs.one); // bye
   console.log(attrs.helloWorld); // hi
    }
}
});
var app = angular.module('MyApp', []);

app.directive('helloWorld', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
            console.log(attrs['helloWorld']);
        }
    }
});