Angularjs 指令属性将布尔值强制转换为字符串

Angularjs 指令属性将布尔值强制转换为字符串,angularjs,Angularjs,我希望在不隔离指令范围的情况下将值传递给指令 My指令有一个链接函数来获取属性值: link: function(scope, element, attrs, ctrl) { scope.myAttributeValue = attrs.myAttribute; } 我想做什么 <directive my-attribute="{{true}}"></directive> 当我将布尔值传递给我的属性时,它将转换为字符串 如何将布尔值传递给我的属性?使用

我希望在不隔离指令范围的情况下将值传递给指令

My指令有一个链接函数来获取属性值:

link: function(scope, element, attrs, ctrl) {

    scope.myAttributeValue = attrs.myAttribute;

}
我想做什么

<directive my-attribute="{{true}}"></directive>

当我将布尔值传递给我的属性时,它将转换为字符串


如何将布尔值传递给
我的属性

使用
范围。$eval

app.directive("myDirective", function(){
    return function linkFn(scope,elem,attrs) {
        var x = scope.$eval(attrs.myDirective);
        console.log(x);
        console.log(typeof x);
    }
});
HTML


将字符串求值为,并将保留该类型

避免使用插值(双花括号
{{true}}
)将角度表达式转换为字符串

有关详细信息,请参阅


使用
范围。$eval

app.directive("myDirective", function(){
    return function linkFn(scope,elem,attrs) {
        var x = scope.$eval(attrs.myDirective);
        console.log(x);
        console.log(typeof x);
    }
});
HTML


将字符串求值为,并将保留该类型

避免使用插值(双花括号
{{true}}
)将角度表达式转换为字符串

有关详细信息,请参阅


尝试将值与布尔值进行比较,值将转换为布尔值:

例如

scope.myAttributeValue = attrs.myAttribute == true;

console.log(typeof scope.myAttributeValue);
console.log(scope.myAttributeValue);

希望有帮助

尝试将值与布尔值进行比较,值将转换为布尔值:

例如

scope.myAttributeValue = attrs.myAttribute == true;

console.log(typeof scope.myAttributeValue);
console.log(scope.myAttributeValue);

希望有帮助

你试过了吗?
my attribute=“true”
?此外,您的链接函数拼写错误(myAtribute缺少一个t)。您可能需要使用
scope.myAttributeValue=attrs.myAttribute!='假'。一种可能的方法是尝试添加布尔逻辑,强制它为布尔逻辑而不是字符串?e、 g.true==truescope.myAttribute使用什么绑定?@,=或&。签入属性签名。如果您使用@,则该值将作为字符串传递。@Danielo,welle none,因为我正在为我的指令定义一个范围……attrs有字符串。使用作用域:{myAttribute:'='},并更正键入错误。您是否尝试过
myAttribute=“true”
?此外,您的链接函数拼写错误(myAtribute缺少一个t)。您可能需要使用
scope.myAttributeValue=attrs.myAttribute!='假'。一种可能的方法是尝试添加布尔逻辑,强制它为布尔逻辑而不是字符串?e、 g.true==truescope.myAttribute使用什么绑定?@,=或&。签入属性签名。如果您使用@,则该值将作为字符串传递。@Danielo,welle none,因为我正在为我的指令定义一个范围……attrs有字符串。使用作用域:{myAttribute:'='},并更正打字错误