angular - 绑定属性值上的 Angular 组件选择器
问题描述
我正在尝试在属性值上编写组件选择器,如下所示:
selector: '[data-type=Type1]'
这在以下 HTML 中效果很好:
<div *ngFor="let item of items">
<div data-type="Type1"></div>
</div>
但不能这样工作(这当然是我想要的):
<div *ngFor="let item of items">
<div [data-type]="item.type"></div>
</div>
我猜这是由于 Angular 解决问题的顺序。我想要的可能吗?
解决方案
不能根据变更检测添加或删除指令。该指令要么始终存在,要么不存在。
您的指令应将值作为输入data-type
,然后确定是否执行其工作。
例如
@Directive({
selector: '[data-type]'
})
export class DataTypeDirective {
@Input('data-type') dataType: string;
doSomeStuff() {
if (this.dataType !== 'Type1') {
return;
}
// perform the directive's function
}
}
或者,如果它是一个组件,*ngIf
请在模板中使用 an:
<div *ngIf="dataType === 'Type1'">
<!-- the component's body -->
</div>
推荐阅读
- xamarin - Xamarin.Forms 获取单击集合视图
- android - ImageButton 与暗模式不匹配
- c# - JsonSerializer.Deserialize 在反序列化不同的类时预计会抛出异常
- python - 为什么我在 tkinter 中的树视图显示了这个?
- wordpress - 看不到在 Wordpress 登录页面中输入的内容
- python - 从 .txt 文件中读取时,撇号出现乱码
- arrays - Julia 使用大型数组,使用 Int8 与 Int64 是否有任何内存/速度优势?
- spring-batch - Kubernetes:通过休息调用调用 cronjob
- python - Django MySQL 原始
- typescript - 在对象类型中查找定义的值