[英]Angular 9 - Form not valid onInit if input autofilled by Browser
[英]Use value of @Input to other variable without onInit in angular 4
我想使用@Input
值在angular 4中初始化其他变量
export class test {
@Input() id: string;
condition: boolean = (this.id == 'myid') //Getting this.id as undefined
}
您的@Input
首先可在ngOnInit
。 因此,您不能在ngOnInit
调用之前访问它。
您可以在类本身内部直接使用id
变量(例如id
代替this.id
仅当变量出现在function
内部时,才需要使用this
变量。 但这最终无济于事,因为id
值始终是undefined
。
理想情况下,您应该在ngOnInit
挂钩中使用此值, ngOnInit
挂钩在组件初始化时会触发,并且您会看到从使用者组件传递的值。
export class test {
@Input() id: string;
condition: boolean;
ngOnInit(){
this.condition = (this.id == 'myid')
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.