简体   繁体   English

如何在Angular中从外部调用组件的功能

[英]How to call a function of component from outside in Angular

How to invoke a function of an Angular Component from parant component. 如何从parant组件调用Angular组件的功能。 What I am trying to achieve here is to get a confirmation message before closing the modal in the parant component. 我在这里想要实现的是在关闭parant组件中的模态之前获取确认消息。

The function inside the child component will check for unsaved changes. 子组件内的函数将检查未保存的更改。 And if there are any unsaved changes, child will call then another modal for confirmation. 如果有任何未保存的更改,孩子将调用另一个模式进行确认。

Upon confirmation from this internal modal, I will then call back again the modal in the parant to invoke a this.autoPopupUnratedModal.dismiss() call 在从这个内部模态确认后,我将再次回调parant中的模态以调用this.autoPopupUnratedModal.dismiss()调用

This is kinda coupled with mess but I am not able to find a clear solution. 这有点混乱,但我无法找到明确的解决方案。 Although there are many answeres to this question, nothing seems to solve my problem. 虽然对这个问题有很多回答,但似乎没有什么能解决我的问题。 Maybe I am look at the wrong sources. 也许我看错了消息来源。

<modal #autoPopupUnratedModal size="lg" [keyboard]="'false'" [backdrop]="'static'">
    <modal-header [show-close]="true">
        <h2 class="modal-title">List of unrated jobs</h2>
    </modal-header>
    <modal-body>

        <unrated-job-notification #unratedNofitication
                 [(onDiscard)]="isCloseNotification"
                 (onDiscard)="onCloseConfirmation($event)">
        </unrated-job-notification>

    </modal-body>
    <modal-footer [show-default-buttons]="false">
        <button type="button" class="btn" (click)="rateAnotherTime()">
           Rate another time
        </button>
    </modal-footer>
</modal>

To call a child's function from it's parent, you can use viewchild to get it's reference or a service with of rxjs observable implementation. 要从其父级调用子级函数,可以使用viewchild获取它的引用或具有rxjs可观察实现的服务。

Using viewchild will suit your problem Here is a sample child and parent component where the parent is calling foo() method of it's child component 使用viewchild将适合您的问题这是一个示例子和父组件,其中父级调用它的子组件的foo()方法

Child component 儿童组件

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

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent {

  constructor() { }

  foo() {
    // I will be called from my parent
  }

}

Parent component 父组件

<app-child #child_comp></app-child>




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

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
  @ViewChild('child_comp') childComp;
  constructor() { }

  ngOnInit() {
    this.childComp.foo();
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2 - 如何从应用程序外部调用组件函数 - Angular2 - how to call component function from outside the app 从外部角度调用组件函数 - angular call component function from outside Angular2 - 从另一个独立组件调用 function。 基本上,从外部组件调用 function - Angular2 - call a function from another standalone component. Basically, call function from outside component 在 angular 指令组件之外调用 function - Call function outside of angular directive component 如何从外部调用angular2组件方法并对此进行参考? - How to call angular2 component method from outside and have reference to this? 如何从 angular 中的其他组件调用 function - How to call a function from other component in angular 如何从另一个组件调用函数 [Angular] - How to call a function from another component [Angular] 如何从 angular 中的 model 组件调用另一个组件 function? - How to call another component function from model component in angular? Angular2:如何从子组件调用主组件函数 - Angular2 : how to call main component function from a child component 如何从 Angular 中的另一个组件调用组件的 ngOnit function - How to call ngOnit function of a component from another component in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM