2016-10-25 123 views
1

我正在使用Angular2和bootstrap作为组件。 如果来自* ngFor循环的测试属性“2”等于名为newTest的导入变量,我试图向我的tr-tag添加一个类。 但它没有添加类。Angular 2 - * ngFor with conditional class

这里是我的组件:

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"); 
       } 
      }) 
     ) 
+0

为什么你有'[class.newDevice]'和'[attr.class]'?这两个不是彼此重叠吗? – martin

+1

AFAIK'attr.class'应该避免(例如在Safari移动中导致问题)。改用'[ngClass]'。 –

+0

你可以试试这个:[ngClass] =“{'newDevice':test.2 == newTest}”'? – rinukkusu

回答

3

[class.newDevice][attr.class]被覆盖对方。