首页 文章

从父类调用子组件方法 - Angular

提问于
浏览
38

我创建了一个子组件,它有一个我想要调用的方法 .

当我调用这个方法时它只触发 console.log() 行,它不会设置 test 属性??

以下是我的更改快速启动Angular应用程序 .

家长

import { Component } from '@angular/core';
import { NotifyComponent }  from './notify.component';

@Component({
    selector: 'my-app',
    template:
    `
    <button (click)="submit()">Call Child Component Method</button>
    `
})
export class AppComponent {
    private notify: NotifyComponent;

    constructor() { 
      this.notify = new NotifyComponent();
    }

    submit(): void {
        // execute child component method
        notify.callMethod();
    }
}

孩子

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'notify',
    template: '<h3>Notify {{test}}</h3>'
})
export class NotifyComponent implements OnInit {
   test:string; 
   constructor() { }

    ngOnInit() { }

    callMethod(): void {
        console.log('successfully executed.');
        this.test = 'Me';
    }
}

如何设置 test 属性?

3 回答

  • 14

    您可以使用 @ViewChild 获取更多信息来检查此link

    使用类型选择器

    @Component({
      selector: 'child-cmp',
      template: '<p>child</p>'
    })
    class ChildCmp {
      doSomething() {}
    }
    @Component({
      selector: 'some-cmp',
      template: '<child-cmp></child-cmp>',
      directives: [ChildCmp]
    })
    class SomeCmp {
      @ViewChild(ChildCmp) child:ChildCmp;
      ngAfterViewInit() {
        // child is set
        this.child.doSomething();
      }
    }
    

    使用字符串选择器

    @Component({
      selector: 'child-cmp',
      template: '<p>child</p>'
    })
    class ChildCmp {
      doSomething() {}
    }
    @Component({
      selector: 'some-cmp',
      template: '<child-cmp #child></child-cmp>',
      directives: [ChildCmp]
    })
    class SomeCmp {
      @ViewChild('child') child:ChildCmp;
      ngAfterViewInit() {
        // child is set
        this.child.doSomething();
      }
    }
    
  • 81

    这对我有用!对于Angular 2,在父组件中调用子组件方法

    Parent.component.ts

    import { Component, OnInit, ViewChild } from '@angular/core';
        import { ChildComponent } from '../child/child'; 
        @Component({ 
                   selector: 'parent-app', 
                   template: `<child-cmp></child-cmp>` 
                  }) 
        export class parentComponent implements OnInit{ 
            @ViewChild(ChildComponent ) child: ChildComponent ; 
    
            ngOnInit() { 
               this.child.ChildTestCmp(); } 
    }
    

    Child.component.ts

    import { Component } from '@angular/core';
    @Component({ 
      selector: 'child-cmp', 
      template: `<h2> Show Child Component</h2>
    <p> {{test }}</p> ` }) export class ChildComponent { test: string; ChildTestCmp() { this.test = "I am child component!"; } }

  • 7

    我认为最简单的方法是使用Subject . 在下面的示例代码中,每次调用'tellChild'时都会通知子项 .

    Parent.component.ts

    import {Subject} from 'rxjs/Subject';
    ...
    export class ParentComp {
        changingValue: Subject<boolean> = new Subject();
        tellChild(){
        this.changingValue.next(true);
      }
    }
    

    Parent.component.html

    <my-comp [changing]="changingValue"></my-comp>
    

    Child.component.ts

    ...
    export class ChildComp implements OnInit{
    @Input() changing: Subject<boolean>;
    ngOnInit(){
         this.changing.subscribte(v => {
         console.log('value is changing', v);
      });
    }
    

相关问题