1

创建按钮时是否可以调用函数。这样我就可以检查功能是否必须禁用按钮

多谢!

4

2 回答 2

0

您可以创建一个指令来检测元素是否已创建 snd 然后基于该指令运行一个函数

import { Directive , Output ,EventEmitter } from '@angular/core';

@Directive({
  selector: '[created]'
})
export class CreatedDirective {

     @Output() created:EventEmitter<any> = new EventEmitter();

   ngAfterViewInit() {
     this.created.emit()
   }

}

演示

另一种使用ViewChildrenViewChild的方法在这里检查我的答案

于 2019-04-02T11:20:19.250 回答
-1

您可以使用@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
}
于 2019-04-02T11:21:16.387 回答