视图和数据,只要一方发生变化,另一方跟着变化。
好处是不需要在代码中手动更新视图,简化开发,增加代码内聚性,代码可读性更强。
缺点是当绑定的数据层次深、数据量大时,会影响性能。
双向数据绑定的语法是[(x)]
.
修改article.component.html
中的内容如下:
<input type="text" [(ngmodel)] = "content">{{content}}
当在input框中输入内容时,插值表达式的位置内容会同时改变。在使用ngmodel
时需要在app.module.ts
中增加formsmodule
的引用。修改app.module.ts
的内容如下:
//在文件头部增加如下一行:import {formsmodule} from假期工资 "@angular/forms";//在imports中增加formsmoduleimports: [ browrmodule, formsmodule ]
article.component.ts
中定义一个布尔类型的值,然后定义一个函数,如下:
export class articlecomponent implements oninit { status = fal; changestatus(){ this.status = true; }}
article.component.html
定义内容如下:
<button class="btn btn-sm btn-info" (click)="changestatus()">更改状态</button><p *ngif="status"> 默认状态下这段话是不显示的,因为status值为fal,当单击上面的按钮, 把status的值设为true时,这段话才显示。</p>
则页面显示效果如<p>
标签中的内容所示。
修改article.component.ts
的内容如下:
<p *ngif="status;el p1"> 默认旅游风光摄影状态下这段话是不显示的,因为status值为fal。</p><ng-template #p1教研室工作总结> <p>如果上面那段话不显示,则表示执行el逻辑,显示这一段话。</p></ng-template>
则页面上初始化时只显示第二段话,表明执行的是el逻辑。ng-template
指令后面会讲到。
下面是内联样式和类样式的写法:
<style> .bg{ background-color: pink; }</style><p [ngclass]="{bg:true}">这段内容应用的是类样式。</p>&l浙江工商大学排名t;p [ngstyle]="{backgroundcolor:getcolor()}">本段内容样式是内联样式。</p>
页面显示效果如下:
article.component.ts
中定义一个数组:
export class articlecomponent implements oninit { articles = ['第一篇文章','第二篇文章', '第三篇文章']}
article.component.html
中通过循环指令输湘钢一中出数组内容:
<p *ngfor="let article of articles; let i = index"> {{i}} - {{article}}</p>
其中的i
为循环下标。页面效果如下所示:
ng-template的说明
ng-template
指令用来定义模板,如下代码所示:
<ng-template #p1> <p>段落内容</p></ng-template>
上面定义了一个简单的模板,id为p1,别的地方可以通过id来引用这个模板。
本文发布于:2023-04-03 02:13:04,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/26a644da2318f633e2d070e6db979468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Angular7教程.doc
本文 PDF 下载地址:Angular7教程.pdf
留言与评论(共有 0 条评论) |