Angular 2: Component Interaction, optional input parameters

前端 未结 3 2038
半阙折子戏
半阙折子戏 2021-02-06 20:39

I have an implementation where parent wants to pass certain data to child component via the use of @Input parameter available at the child component. However, this

3条回答
  •  温柔的废话
    2021-02-06 21:05

    You have two options here.

    1) You can use an *ngIf on the child in case the child does not need to be displayed when its Input is empty.

     
         //passing parameter
         //not willing to passing any parameter
    
    

    2) In case the child should get displayed without any input, you can use a modified setter to check for the presence of input variables`

    In the child.ts:

    private _optionalObject: any;
    @Input()
    set optionalObject(optionalObject: any) {
        if(optionalObject) this._optionalObject = optionalObject;
    }
    get optionalObject() { return this._optionalObject; }
    

提交回复
热议问题