金狮镖局 Design By www.egabc.com
Angular 中常用的指令有用来遍历的 *ngFor
、控制元素显示隐藏的 *ngIf
,今天学习一下 *ngIf
这个常用的指令。
NgIf 指令
ngIf 指令用于根据表达式的值,在指定位置渲染then 或 else 模板的内容。
then 模板除非绑定到不同的值,否则默认是 ngIf 指- 令关联的内联模板。
else 模板除非绑定对应的值,否则默认是 null。
简单形式
<div *ngIf="condition">...</div> <!--Angular 2.x中使用template--> <ng-template [ngIf]="condition"><div>...</div></ng-template>
else
<div *ngIf="condition; else elseBlock">...</div> <ng-template #elseBlock>...</ng-template>
then 和 else
<div *ngIf="condition; then thenBlock else elseBlock"></div> <ng-template #thenBlock>...</ng-template> <ng-template #elseBlock>...</ng-template>
在我们的实际业务中可能遇到这样的需求,一个 table
表格,最后一列有修改、删除或者其他操作,当我们点击修改按钮的时候,当前这一行的内容都出现在一个 input
输入框里面,然后我们可以直接进行修改,这个时候我们就可以使用 *ngIf
和 else
来实现。效果图如下:
部分实现代码:
<tr *ngFor="let item of gridList"> <td *ngIf="item.bol; else inputid">{{item.id}}</td> <ng-template #inputid> <td class="insert"><input type="text" [value]="item.id"></td> </ng-template> ... </tr>
这里的 inputid
可以理解为一个模板 id
,它指向 <ng-template #inputid>
这个模板,当 item.bol
为 false
时,angular就会找到这个模板里的内容进行替换。
注意这个模板 id 是唯一的,如果多次使用 *ngIf else
指令需要使用不同的 id。
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无angular *Ngif else用法详解的评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】