Angular里的structural directive的一个例子

简介: Angular里的structural directive的一个例子

在product-list.component.html里,编写如下代码:

<h2>Jerry Products</h2>
<div *ngFor="let product of products">
  <h3>
      {{ product.name }}
  </h3>
</div>

image.png

这些product的测试数据编写在文件products.ts里:image.png

在product-list.component.ts里,导入Component ProductListComponent:


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XMPlQrx7-1597136299073)(https://upload-images.jianshu.io/upload_images/2085791-00151ff1a589c8e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]


两个大括号内部的语法:


let <单数形式> of <复数形式>

image.png

复数形式可以在product-list.component.ts的export里自定义:

image.png

更高级的用法:

<h2>Jerry Products</h2>
<div *ngFor="let product22 of productsJerry2">
    <h3>
    <a [title]="product22.name + ' details'">
      {{ product22.name }}
    </a>
  </h3>
</div>

效果图:

image.png

相关文章
|
5月前
Angular启动/node_modules/@types/node/index.d.ts (20,1): Invalid ‘reference‘ directive syntax.
Angular启动/node_modules/@types/node/index.d.ts (20,1): Invalid ‘reference‘ directive syntax.
59 2
从 Angular Component 和 Directive 的实例化,谈谈 Angular forRoot 方法的命令由来
同 Angular service 的单例特性不同,Angular 组件和指令通常会被多次实例化,比如 HTML markup 中每出现一次 Component 的 selector,就会触发 Component 的一次实例化。
|
JavaScript
Angular1.x的自定义指令directive参数配置详细说明
Angular1.x的自定义指令directive参数配置详细说明
关于调用 Angular 属性指令 attribute Directive 是否需要带中括号的问题
关于调用 Angular 属性指令 attribute Directive 是否需要带中括号的问题
148 0
关于调用 Angular 属性指令 attribute Directive 是否需要带中括号的问题
|
JavaScript
利用 Angular Directive 和 @HostBinding 实现输入文本框随着键盘输入自动变色效果
利用 Angular Directive 和 @HostBinding 实现输入文本框随着键盘输入自动变色效果
利用 Angular Directive 和 @HostBinding 实现输入文本框随着键盘输入自动变色效果
一个关于Angular Directive selector里的中括号使用问题
一个关于Angular Directive selector里的中括号使用问题
一个关于Angular Directive selector里的中括号使用问题
|
JavaScript
关于Angular directive使用的语法问题
前者用于控制或者改变 DOM,后者控制已有 elements 的外观或者行为。
关于Angular directive使用的语法问题
Angular jasmine fixture.detectChanges如何触发directive的set方法
Angular jasmine fixture.detectChanges如何触发directive的set方法
104 0
Angular jasmine fixture.detectChanges如何触发directive的set方法
每个施加在HTML元素上的Angular Directive,运行时都会生成一个新的实例
每个施加在HTML元素上的Angular Directive,运行时都会生成一个新的实例
94 0
每个施加在HTML元素上的Angular Directive,运行时都会生成一个新的实例
Angular自定义structural指令的实例化过程以及set方法的调用
Angular自定义structural指令的实例化过程以及set方法的调用
104 0
Angular自定义structural指令的实例化过程以及set方法的调用
下一篇
无影云桌面