【发布时间】:2018-05-24 18:42:02
【问题描述】:
使用“@angular/core”:“^5.0.1”、“@angular/material”:“^5.0.0-rc.1”、“@angular/cdk”:“^5.0.0-rc .1"。
我具有检查所有字段是否添加了 XXXqaName 属性的功能,因此添加了一个带有 @input 属性的指令,该指令接受 XXXqaName
选择器 mat-select 元素的静态输入,如 qa-name="matselectone" 工作正常,并且 XXXqaName 属性出现在 HTML 模板中,如:
<mat-select XXXqaName="matselectone"></mat-select> // Works fine
当添加 mat-options 的动态值 像 XXXqaName="data-{{ option.value }}" 输入不是通过(即未定义)和 XXXqaName 属性在像
这样的 HTML 模板中找不到<mat-option XXXqaName="data-{{ option.value }}"></mat-option> // XXXqaName missing in HTML template only ng-reflect name is present.
注意:option.value 存在,并且下拉列表中的选项值已填充,但 HTML 中缺少属性“xxxqaName”。
指令:
@Directive({
selector: `input[type="text"], mat-select, mat-option`
})
export class QaDirective implements OnInit {
@Input('XXXqaName') qaName: string;
constructor(private element: ElementRef) { }
ngOnInit() { this.verifyQa();}
verifyQa(): void {
if (!this.qaName) {
console.error('No "XXXqaName" provided for:',This.element.nativeElement);
return;
}
}
}
为输入属性值传递动态值时的指令仍未定义。是否有其他方法可以将动态值传递给指令任何帮助都会很棒。
注意:可能看起来像Dynamic attribute values for directives,但它们使用组件中的函数来操作输入。这里略有不同,属性仍未定义。
【问题讨论】:
-
When inspecting html template couldn't find attribute当然你不会看到这些属性,因为你绑定到属性 -
是的,不能很好地绑定属性。
-
我在
@Input('qa-name')前面添加了@HostBinding('attr.qa-name')