Html 角2为元素指定动力学类

Html 角2为元素指定动力学类,html,twitter-bootstrap,angular,Html,Twitter Bootstrap,Angular,我是Angular 2的新手,我想给HTML元素分配一个类。 它应该是动态-->如果另一个元素(具有id)具有属性,则该元素应成为类 这是我试过的代码: <div class="panel panel-default" ngFor="let date of epgDates; let i = index"> <div class="panel-heading" role="tab"> <h4 class="panel-title">

我是Angular 2的新手,我想给HTML元素分配一个类。 它应该是动态-->如果另一个元素(具有id)具有属性,则该元素应成为类

这是我试过的代码:

<div class="panel panel-default" ngFor="let date of epgDates; let i = index">
    <div class="panel-heading" role="tab">
        <h4 class="panel-title">
            <a #aEle role="button" data-toggle="collapse" id="a-{{i}}" href="#{{i}}" aria-expanded="true"> 
                <span class="glyphicon" [class.glyphicon-minus]="aEle.getAttribute('aria-expanded')==='true'"
                    aria-hidden="true">
                </span> {{date | date: 'dd.MM.yyyy'}}
            </a>
        </h4>
    </div>
</div>

使用此代码,我得到一个错误…:/ 有人能帮我吗


非常感谢

您不能同时使用
[]
{{}
。一个或另一个,但不是两者都有

“true”
是返回
true
的表达式时,设置该类,否则将删除该类

[class.glyphicon减号]=“真”
这将在表达式
true
返回
true
时设置类
glyphicon减号
,否则将删除该类,并在第二个表达式返回
true
时设置或删除类
#a-1。否则将删除该类(假设
i
1
)。 类名可以是字符串(
“glyphicon-减号”
)或表达式(
“#a-”+i+”。aria expanded“

[ngClass]=“{'glyphicon-minus':true',#a-'+i+'.扩展的咏叹调:true}”
表达式
true
可以是一个文本布尔值(如图所示)或组件类属性的名称,也可以是一个包含运算符和函数调用的更复杂的表达式

更新



我将
#aEle
模板属性添加到
中,并使用它获得
aria expanded
属性值。

预期结果是什么
[class.glyphicon-minus]
表示要添加/删除类
glyphicon-minus
,但我完全不清楚
“#a-{{i}.aria expanded='true'”
应该做什么。如果id为a-{i}(例如a-0)的元素的'aria expanded'-属性为true,则应该添加该类。谢谢!但它仍然失败,出现以下错误:“'Unhandled Promise rejection:Template parse errors:Parser Error:Missing expected:Missing expected:Missing expected:在中[{'glyphicon-minus':true'”、#a-“+i+”.aria expanded':true}]的第33列ProgramComponent@28:104(“][ngClass]=”{'glyphicon-减号:true',#a-'+i+'.aria扩展:true}“aria hidden=”true“>”): ProgramComponent@28:104’第33栏是什么?我找不到错误。我根据您在问题下方的评论更新了我的答案。您更新的答案很好,没有给出错误,但有一个问题:因为''-Elemnt在ngFor中,th'#aEle'是重复的。。。我更新了我原来的帖子,整个部门和ngFor。。。也许你也有解决办法。。。谢谢我认为整个方法是有缺陷的。为什么需要从
元素中读取属性以了解“aria expanded”是否设置为
true
,而不是读取aria expanded
所依赖的属性?或者,如果它总是真的,比如在你的例子中,为什么你需要阅读它呢?嗨,好吧,我试着解释一下:当我点击一个按钮时,a-元素的“aria expanded”属性从真变为假。基于此,我想用类“减号”显示一个span,但是,如果“a”的“aria expanded”-属性为“true”。。。