2016-09-29 60 views
0

在我的應用程序中,我有變量的字符串。哪個屬性的字符串名稱中要添加。如何從可變的角度2設置DOM元素的各種屬性?

ngOnInit() 
{ 
    this.colAttibute = 'width-50'; 
}  

或者它可以等於'寬度-100'。我的應用程序獲取設備寬度並在onNgInit方法上設置此變量的值。 我想這個屬性設置爲我的HTML代碼,這樣的事情:

<ion-list no-lines *ngIf="list"> 
    <ion-list-header> 
    Лидеры продаж 
    </ion-list-header> 
    <ion-item class="categoryProductItem" *ngFor="let row of list;"> 
     <ion-row wrap > 
     <ion-col *ngFor="let product of row" $colAttibute > <!--Here must be width-50 or width-100 attribute--> 
      <a ion-item detail-push (click)="onSelectItem(product)" > 
      <div class="categoryProductItemImage" *ngIf="product.getMainImage()"> 
       <ion-img [src]="product.getMainImage()['small_url']"></ion-img> 
      </div> 
      <h2> 
       {{product.title}} 
      </h2> 
      </a> 
     </ion-col> 
     </ion-row> 
    </ion-item> 
</ion-list> 

如何做到這一點?

回答

1

您可以設置寬度是這樣的:

<ion-col [attr.width-70]="flag"> 

凡標誌是表示是否要將此屬性或不是一個布爾值。 不幸的是,您需要列出所有可用的屬性,以動態支持所有屬性。

+0

注意:要取消屬性,需要將'flag'設置爲'null'。 'false'不會將其刪除。 –

+0

我可以以某種方式在模板中使用「this」嗎? –