*ngIf 里面有一个 input :
*ngIf
input
<div *ngIf='display'> <input number="number" /> </div>
我想检测 input 何时显示或不在数字指令内 . 这意味着当 display=true 并更改为 false 时,它应该调用指令内的函数以及返回 true 时的函数 .
display=true
false
true
这是plunker
每次 display 从 false 更改为 true 时都会创建 number 指令,并在 display 从 true 更改为false时销毁 .
display
number
对于前一种情况,您可以使用 constructor(...) , ngOnInit() , ngAfterViewInit() 或 ngAfterContentInit() ,
constructor(...)
ngOnInit()
ngAfterViewInit()
ngAfterContentInit()
对于后来 ngOnDestroy() 可以使用 .
ngOnDestroy()
1 回答
每次
display
从false
更改为true
时都会创建number
指令,并在display
从true
更改为false时销毁 .对于前一种情况,您可以使用
constructor(...)
,ngOnInit()
,ngAfterViewInit()
或ngAfterContentInit()
,对于后来
ngOnDestroy()
可以使用 .