简体   繁体   English

Angular 10:从父组件调用子组件函数

[英]Angular 10: Call a child component function from parent component

I have 2 component parent (Login Screen) and a child (user-list).我有 2 个组件父级(登录屏幕)和一个子级(用户列表)。 Parent component has dropdowlist.父组件有下拉列表。 The grid loads according to the item chosen in the drop down list I need to fire function of the child component and this is not working for me.网格根据下拉列表中选择的项目加载我需要触发子组件的功能,这对我不起作用。 I have the following code: parent component html:我有以下代码:父组件html:

I have the following code:

 <div>[items]="UserTypeSelectItems"[(ngModel)]="UserTypeId" id="fieldType"
 bindLabel="value" bindKey="key" (change)="changeUserType()" [clearable]="false">
 </div>
 <app-user-list></app-user-list>

parent component ts:父组件 ts:

I have the following code:我有以下代码:

export class Login-ScreenComponent implements OnInit {
@ViewChild(UserListComponent)child:UserListComponent;
userTypeSelectItems: Array<SelectItem>;
userTypeId: any;
items: any;
constructor(    
private userTypeSettingsService: userTypeSettingsService,

) {
this.userTypeSettingsService.getuserTypes().subscribe((data) => {
  this.userTypeSelectItems = data;
  if (
    this.userTypeSelectItems &&
    this.userTypeSelectItems.length > 0
  ) {
    this.userTypeId =
      this.userTypeSettingsService.selectedContractTypeId ??
      this.userTypeSelectItems[0].key;
    this.userTypeSettingsService.setContractTypeId(this.contractTypeId);
    this.userTypeSettingsService.fillSelectedFields(this.userTypeId).subscribe(dataFields => {
      this.items = dataFields;
      this.child.getUser();
       });
      }
      });
       }
      changeUserType() {
      this.child.getUser();
       }

child component ts:子组件 ts:

I have the following code:我有以下代码:

getUser() {
this.loading = true;
this.userService
  .getAllUsers(this.userTypeId)
  .pipe(finalize(() => (this.loading = false)))
  .subscribe(
    (data) => {
      this.rows = data.map(notif => {
        return {
          user_status_id: status_id,     
      });
    },
    (err) => this.toastr.error(err),
    () => (this.loading = false)
  );

} ''''''' } '''''''

if I understand your question correctly, that's what I'd suggest如果我正确理解你的问题,那就是我的建议

Parent HTML父 HTML

<div>[items]="UserTypeSelectItems"[(ngModel)]="UserTypeId" id="fieldType"
 bindLabel="value" bindKey="key" (change)="changeUserType()" [clearable]="false">
 </div>
 <app-user-list [userTypeId]="userTypeId"></app-user-list>

In the parent ts remove all the calls of the this.child.getUser()在父 ts 中删除 this.child.getUser() 的所有调用

In the child component you should have input parameter userTypeId with setter.在子组件中,您应该使用 setter 输入参数userTypeId It will invoke the getUser() function every time when value is changed.每次更改值时,它都会调用getUser()函数。

private _userTypeId: number;
@Input() 
get userTypeId(): number {
  return this._userTypeId;
}
set userTypeId(value: number): void {
  this._userTypeId = value;
  this.getUser();
}

You also can use the external service which will be injected in the parent and child components or use some Subject in the parent component, create Observable base of it which will be sent as input parameter to the child component.您还可以使用将被注入到父组件和子组件中的外部服务或在父组件中使用一些 Subject,创建它的 Observable 基础,它将作为输入参数发送给子组件。 There you subscribe on the observable and then you need to emit the value with subjectvar.next(value) and as result function will be called.在那里你订阅了 observable,然后你需要用subjectvar.next(value)发出值,结果函数将被调用。 I can write down the example if you need.如果你需要,我可以写下这个例子。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM