Angular权威教程之内置指令篇_angular ngswitch default

<div class="container" [ngSwitch]="myVar">   
<div \*ngSwitchCase="'A'">Var is A</div>   
<div \*ngSwitchCase="'B'">Var is B</div>   
<div \*ngSwitchDefault>Var is something else</div> 
</div> 

Default默认值,其他特殊值ngSwitchCase来处理

ngStyle:

[style.<cssproperty>]="value"的形式引入:
 <div [style.background-color]="'red'">        
  Uses fixed red background       
 </div> 

键值对形式引入:
<div [ngStyle]="{color: 'white', 'background-color': 'blue'}">         
 Uses fixed white text on blue background       
</div>

动态引入:
a.定义两个输入框:
 <div class="ui input">         
 <input type="text" name="color" value="{{color}}" #colorinput> </div> 
 
 <div class="ui input">         
 <input type="text" name="fontSize" value="{{fontSize}}" #fontinput> </div> 

b.基于输入框的值来设定字体大小:
 <div>       
 <span [ngStyle]="{color: 'red'}" [style.font-size.px]="fontSize">         
 red text       
 </span>     
 </div> 

c.基于输入框的值来设定颜色:
 <h4 class="ui horizontal divider header">       
 ngStyle with object property from variable     
 </h4> 
 
  <div>       
   <span [ngStyle]="{color: color}">         
   {{ color }} text       </span>     
  </div> 
 
  <h4 class="ui horizontal divider header">       
  style from variable     
  </h4> 
 
  <div [style.background-color]="color" style="color: white;">       
  {{ color }} background     
  </div> 

存在疑惑:动态传输的时候的值命名和引用的名称为何不统一。
在这里插入图片描述

ngClass:

ngClass和ngStyle不同点是在于:
前者为类层次的,后者为属性层次的

括号中键值对,第一个为类名称,第二个为Boolean值:
  <div [ngClass]="{bordered: false}">This is never bordered</div>   
  <div [ngClass]="{bordered: true}">This is always bordered</div> 

动态指令:
<div [ngClass]="{bordered: isBordered}">    
 Using object literal. Border {{ isBordered ? "ON" : "OFF" }}   </div> 

ngFor :

渲染元素:
*ngFor ="let item of items"

for example:

 this.cities = ['Miami', 'Sao Paulo', 'New York']; 

 <div class="ui list" *ngFor="let c of cities">     
 <div class="item">{{ c }}</div>   

将会依次渲染出cities的每个城市。
在这里插入图片描述

渲染表格
this.people = 
 [       
 { name: 'Anderson', age: 35, city: 'Sao Paulo' },       
 { name: 'John', age: 12, city: 'Miami' },       
 { name: 'Peter', age: 22, city: 'New York' }     
 ];

 <table class="ui celled table">     
 <thead>       
 <tr>         
 <th>Name</th>         
 <th>Age</th>         
 <th>City</th>       
 </tr>     
 </thead>     
 <tr \*ngFor="let p of people">       
 <td>{{ p.name }}</td>       
 <td>{{ p.age }}</td>       
 <td>{{ p.city }}</td>     
 </tr>   
 </table> 

在这里插入图片描述

ngNonBindable:

不要编译或者绑定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值