Angular 2 - * ngFor with conditional class

时间:2016-10-25 08:02:48

标签: angular typescript angular2-template

我正在使用Angular2和bootstrap作为组件。 如果属性" 2"我试图在我的tr-tag中添加一个类。来自* ngFor循环的测试等于名为newTest的导入变量。 但它并没有增加课程。

这是我的组成部分:

loadTest() {
      this.testService.getTests()
         .then((data) => {
            this.tests = data
         })
   }

ngOnInit() {
      this.loadTest();
   }

表格显示正确,但课程不在那里。

但条件肯定是满满的。

这是我的HTML:

<div class="table-responsive">
    <table class="table table-hover table-reflow">
        <thead>
            <tr>
                <th>1</th>
                <th>2</th>
                <th>3</th>
                <th>4</th>
                <th>5</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let test of tests" [class.newDevice]="test.2 === this.newTest" [attr.class]="test.1" >
                <th>{{test.1}}</th>
                <th>{{test.2}}</th>
                <th>{{test.3}}</th>
                <th>{{test.4}}</th>
                <th>{{test.5}}</th>
            </tr>
        </tbody>
    </table>
</div>

我做错了什么吗? 我也尝试用jquery实现同样的目标:

    loadTest() {
          this.testService.getTests()
             .then((data) => {
                this.tests = data
    if (NewTest.length !== undefined) {
                   let index = this.tests.findIndex((e) => { return e.2 === NewTest })
                   let test = this.tests[index].id
                   jQuery(test).addClass("newDevice");
                }
           })
       )

1 个答案:

答案 0 :(得分:3)

[class.newDevice][attr.class]相互重叠。