Angularjs 在html中为指令属性使用值而不是变量

Angularjs 在html中为指令属性使用值而不是变量,angularjs,Angularjs,我想使用具有属性的。我希望此属性始终为true,因此我不需要此属性的范围变量。但我很确定下面的说法是错误的,它可能是一个名为true的范围变量,而不是布尔值true。这个怎么写我想要的 <accordion close-others="true"> </accordion> 编辑:我意识到这是一个坏例子。如果我有一个属性,我想给它赋值“someText”,该怎么办。如何区分值“someText”和名为“someText”的变量?像这样: <some-direc

我想使用具有属性的。我希望此属性始终为true,因此我不需要此属性的范围变量。但我很确定下面的说法是错误的,它可能是一个名为true的范围变量,而不是布尔值true。这个怎么写我想要的

<accordion close-others="true">
</accordion>

编辑:我意识到这是一个坏例子。如果我有一个属性,我想给它赋值“someText”,该怎么办。如何区分值“someText”和名为“someText”的变量?像这样:

<some-directive some-attribute="someText"></some-directive>

您可以在指令中定义角度应如何处理该属性值

这意味着如果您这样定义它,它将被解析为常规javascript:

scope: {
    closeOthers: '='
}

//gives you in scope.closeOthers:
<e close-others="true" /> //true
<e close-others="'someText'" /> //someText
<e close-others="variableName" /> //contentOfVariableName
scope: {
    closeOthers: '@'
}

//gives you in scope.closeOthers:
<e close-others="true" /> //true
<e close-others="'someText'" /> //'someText'
<e close-others="variableName" /> //variableName
<e close-others="prefix/{{variableName}}" /> //prefix/contentOfVariableName
您说过
我希望这个属性始终为true,所以我不需要这个
的范围变量,所以我猜您甚至不需要解析它(如果您不关心{}}表达式的话)。然后你可以简单地做:

链接:函数(范围、元素、属性){
var closeOthers='closeOthers'在属性中;//true或false
}
这允许您使用如下“关闭其他人”:

scope: {
    closeOthers: '='
}

//gives you in scope.closeOthers:
<e close-others="true" /> //true
<e close-others="'someText'" /> //someText
<e close-others="variableName" /> //contentOfVariableName
scope: {
    closeOthers: '@'
}

//gives you in scope.closeOthers:
<e close-others="true" /> //true
<e close-others="'someText'" /> //'someText'
<e close-others="variableName" /> //variableName
<e close-others="prefix/{{variableName}}" /> //prefix/contentOfVariableName


应该可以按照您的方式正常工作,如果不创建不符合预期的演示,如何在使用布尔值true和名为true的变量之间进行更改?Angular是否首先检查是否有一个名为true的变量,如果没有,则将其视为一个值?哦,我刚刚意识到,可能不允许将变量称为“true”。。。对不起,这个愚蠢的问题!我用一个不那么愚蠢的变体更新了这个问题……你试过用单引号把它变成字符串吗?由于html显示的是布尔值,现在editWow的问题就不那么清楚了,这比我预想的要详细得多,我从中学到了很多!非常感谢@MArc:)