pass value from parent to child

后端 未结 2 411
遇见更好的自我
遇见更好的自我 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);
    
    0 讨论(0)
  • 2021-01-15 10:28

    Use Event Emitter. It will call parent component method and you can pass your any object in that method call as below:

    Child Component:

    import {ViewChild, Component, Output, EventEmitter}   from "@angular/core";
    
    @Output() RefreshParentGridView = new EventEmitter();
    
    public Close(): void {       
         let myObject:any; //Your object which pass in parent
         this.RefreshParentGridView.emit(myObject);               
    }  
    

    Parent Component: register output event as below

    HTML

    <childTeg (RefreshParentGridView)= "RefreshGridView($event)"></childTeg>
    

    Backend Code

    public RefreshGridView(model: any)
    {
    }
    
    0 讨论(0)
提交回复
热议问题