如何在Angular 4中为对象属性添加对象值

时间:2018-04-18 12:50:08

标签: html angular typescript

我想在输入标签中添加样式宽度,高度和颜色。



<table>
 <td *ngFor="let y of x" >
      <input  type="{{y.cellNumber}}"  src="{{y.src}}"  value="{{y.name}}" height="{{y.height*2}}" width="{{y.width/2}}" style="height:{{y.height}}px;width:{{y.width}}px">    
 </td> 
</table>    
&#13;
&#13;
&#13;

但是在样式内部无法设置对象值。是否有任何其他方法可以在输入标记内的样式中设置对象值。

2 个答案:

答案 0 :(得分:0)

你可以像这样使用它:

<table>
 <td *ngFor="let y of x" >
      <input 
      type="{{y.cellNumber}}"  
      [src=]"y.src"   
      [value]="y.name" 
      [style.height.px]="y.height/2"  
      [style.width.px]="y.width/2" >    
 </td> 
</table>    

另一种方法是使用ngStyle,如下所示:

<table>
 <td *ngFor="let y of x" >
      <input 
      type="{{y.cellNumber}}"  
      [src=]"y.src"   
      [value]="y.name" 
      [ngStyle]="getStyle(y)">    
 </td> 
</table>    


getSyle({ width, height }) {
  return {
    width: `${width/2}px`,
    height: `${height/2}px`,
  };
}

答案 1 :(得分:0)

您可以使用NgStyle

<table>
 <td *ngFor="let y of x" >
      <input [type]="y.cellNumber" [src]="y.src" 
             [value]="y.name" [height]="y.height*2" [width]="y.width/2" 
             [ngStyle]="{ 'height.px':y.height, 'width.px': y.width}">    
 </td> 
</table>