pass value from parent to child

后端 未结 2 434
遇见更好的自我
遇见更好的自我 2021-01-15 10:17

this is my layout.html

2条回答
  •  灰色年华
    2021-01-15 10:18

    Create a shared interface and set value from child and get from parent as below:

    Interface:

    import {Injectable} from "@angular/core";
    
    export interface SharedModel {
        Name: string;
    }
    
    @Injectable()
    export class Shared {
    
        SharedComponent: SharedModel = {
            Name: ''        
        };
    
        constructor() {       
        }
    }
    

    Parent/Child Component:

    import {Shared, SharedModel}    from '../Shared';
    
    public sharedData: SharedModel;
    constructor(private sharedResource: Shared) {
        this.sharedData = sharedResource.SharedComponent;
    }
    

    Set Value:

    this.sharedData.Name="Sandip Patel";
    

    Get Value:

    console.log(this.sharedData.Name);
    

提交回复
热议问题