Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 重新输入@Input change_Angularjs_Typescript_Angular - Fatal编程技术网

Angularjs 重新输入@Input change

Angularjs 重新输入@Input change,angularjs,typescript,angular,Angularjs,Typescript,Angular,当组件输入更改时,是否可以告诉angular运行init(或任何其他生命周期函数或函数),或者我必须从父组件调用该函数,而不是更改输入 谢谢你的回答 当组件的输入发生更改时,ngOnChangeslifecycle钩子运行。在那里,您可以根据需要调用任何其他生命周期挂钩或/和任何函数 ngOnChanges(...args: any[]) { console.log('onChange fired'); this.ngOnInit(); } ngOnI

当组件输入更改时,是否可以告诉angular运行init(或任何其他生命周期函数或函数),或者我必须从父组件调用该函数,而不是更改输入


谢谢你的回答

当组件的输入发生更改时,
ngOnChanges
lifecycle钩子运行。在那里,您可以根据需要调用任何其他
生命周期挂钩或/和任何函数

ngOnChanges(...args: any[]) {

    console.log('onChange fired');            
    this.ngOnInit();
}

ngOnInit()
{
    console.log('ngOnInit fired');
}
编辑
出于某种原因,如果要从父级调用子级的
ngOnInit
,则可以使用
ViewChild
,如下所示

export class Parent{

   @ViewChild(child) vc:child;

    ngAfterViewInit()
    { 
      console.log("ngAfterInit");
      console.log(this.vc.ngOnInit());
    }

};

export class child{

   ngOnInit()
    {
        console.log('ngOnInit fired');
    }
};

当组件的输入发生更改时,
ngochanges
lifecyclehook运行。在那里,您可以根据需要调用任何其他
生命周期挂钩或/和任何函数

ngOnChanges(...args: any[]) {

    console.log('onChange fired');            
    this.ngOnInit();
}

ngOnInit()
{
    console.log('ngOnInit fired');
}
编辑
出于某种原因,如果要从父级调用子级的
ngOnInit
,则可以使用
ViewChild
,如下所示

export class Parent{

   @ViewChild(child) vc:child;

    ngAfterViewInit()
    { 
      console.log("ngAfterInit");
      console.log(this.vc.ngOnInit());
    }

};

export class child{

   ngOnInit()
    {
        console.log('ngOnInit fired');
    }
};