相关疑难解决方法(0)

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

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

当我调用这个方法时它只会触发该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();
    }
}
Run Code Online (Sandbox Code Playgroud)

儿童

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

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

typescript angular-components angular

94
推荐指数
7
解决办法
12万
查看次数

标签 统计

angular ×1

angular-components ×1

typescript ×1