【问题标题】:Reloading the component with button click from another component in angular 2/4通过从另一个组件以 2/4 角度单击按钮重新加载组件
【发布时间】:2018-01-08 09:13:13
【问题描述】:

我有两个组件,比如 app-root 和 new-component

我在 app-root 组件中有一个按钮。当我点击按钮时,我每次都需要重新加载新组件。

请指导我,如何做到这一点。

谢谢

【问题讨论】:

    标签: angular angular2-routing angular2-services angular2-directives


    【解决方案1】:

    您可以轻松地从父组件刷新组件,方法是在组件内创建一个 reload 方法并从 prent 调用,如下所示,

    @Component({
      selector: 'my-app',
      template: `<h1>Hello {{name}}</h1>
      <my-child #child ></my-child>
      <button (click)="child.refresh()" >Refresh Child</button>
      `
    })
    export class AppComponent { 
      name = 'World'; 
    }
    
    @Component({
      selector: 'my-child',
      template: `<h1>Date : {{date}}</h1>`
    })
    export class ChildComponent { 
      date = new Date();
      
      refresh(){
        this.date = new Date();
      }
    }
    

    检查这个Plunker!!

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 2020-06-13
      • 2020-04-30
      • 2017-12-31
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      相关资源
      最近更新 更多