创建按钮时是否可以调用函数。这样我就可以检查功能是否必须禁用按钮
多谢!
您可以创建一个指令来检测元素是否已创建 snd 然后基于该指令运行一个函数
import { Directive , Output ,EventEmitter } from '@angular/core';
@Directive({
selector: '[created]'
})
export class CreatedDirective {
@Output() created:EventEmitter<any> = new EventEmitter();
ngAfterViewInit() {
this.created.emit()
}
}
另一种使用ViewChildren或ViewChild的方法在这里检查我的答案
您可以使用@ViewChild()
and 检查值来查看按钮是否存在。
就像是:
在模板中:
<input type='button' #button />
在组件中:
@ViewChild('button') someButton;
在功能上:
if (this.someButton){
// do something with someButton
// You might want to do this.someButton.nativeElement and convert to HTMLelement to get the element as button
}