【问题标题】:Passing a callback object from parent to child component angular将回调对象从父组件传递给子组件
【发布时间】:2020-08-27 16:00:48
【问题描述】:

我有一个作为父组件的表单组件和一个作为子组件的显示结果组件。 我调用了一个 api,我必须使用回调来检索数据。 在我的服务层我打电话:

postSearchDocument(response: any, callback): void {
  console.log('Response form in service layer: ', response);

  const xmlhttp = new XMLHttpRequest();
  xmlhttp.open('POST', this.appConfig.getConfig().apiUrl + '' + this.appConfig.getConfig().searchDocument, true);

  // build SOAP request
  const soapRequest =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
    'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
    'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
    'xmlns:bran="http://www.bottomline.com/soap/branch/">' +
    '<soapenv:Header/>' +
    '<soapenv:Body>' +
    '<bran:CallBranch soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
    '<JSONRequest xsi:type="xsd:string">' +
    JSON.stringify(response) +
    '</JSONRequest>' +
    '</bran:CallBranch>' +
    '</soapenv:Body>' +
    '</soapenv:Envelope>';

  console.log('SOAP REQUEST ');
  console.log(soapRequest.toString());

  xmlhttp.onreadystatechange = () => {
    if (xmlhttp.readyState === 4) {
      if (xmlhttp.status === 200) {
        this.returnValue = xmlhttp.responseText;
        // alert(xmlhttp.responseText);
        console.log('Response : ');
        console.log(xmlhttp.responseText);

        this.documentResponse = JSON.parse(this.returnValue)
      );  
      console.log(this.documentResponse);
      callback.apply(this, [this.documentResponse]);
      // return this.documentResponse;
    }
  }
};

显然我将数据从父级传递给子级:

<app-show-results [documentsResponse]="documentsResponse"></app-show-results>

在父组件中,我有一个 onSubmit 方法,允许我调用我的 API:

this.apiService.postSearchDocument(this.searchRequest, this.getResponse);

这是我的回调:

getResponse(response): void {
  this.documentsResponse = response;
  console.log('In callback response ', this.documentsResponse);
}

此 API 调用使用 SOAP,我添加了一个回调以便从服务器获取响应:

在我的子组件中,我确实有这个变量:

@Input() documentsResponse;

我的问题是我没有在子组件中显示来自父级的返回值。我在我的子组件中添加了一个生命周期挂钩来监控更改:

ngOnChanges(changes: SimpleChanges): Promise<any> {
  if (changes.documentsResponse && this.documentsResponse !== null) {
    console.log(this.documentsResponse);
  }
}

【问题讨论】:

  • postSearchDocument 是什么样的......?
  • 您使用的是什么类型的changeDetectionStrategy?在父子组件中?
  • 您也可以发布父子打字稿代码吗?检查确切的问题
  • 我想我正在使用默认的 changeDetectionStrategy ...我没有设置任何这样的东西。我更新了代码
  • 你能添加你在@Component() 装饰器中的代码吗

标签: angular callback lifecycle


【解决方案1】:

当您使用ChangeDetectionStrategy.OnPush 时,更改检测将受到限制,仅适用于某些情况。您需要标记父组件和子组件以便在下一个周期中检查。试试下面的代码

父组件

import { ..., ChangeDetectorRef } from '@angular/core'

@Component({})
class ParentComponent {
  documentResponse: any

  ...

  constructor(
    private changeDetectorRef: ChangeDetectorRef
  ) {}

  ...

  getResponse(response) {
    this.documentResponse = response;
    this.changeDetectorRef.markForCheck(); // Here is the fix
  }
}

工作stackblitz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2016-05-10
    • 2018-11-30
    • 2018-01-01
    • 2021-07-12
    相关资源
    最近更新 更多