如果未定义字段,Angular不会检测到更改

时间:2019-02-05 14:56:28

标签: angular angular2-changedetection

Angular不会检测到更改,并且在没有我手动取消定义我之前要更​​新的字段的情况下更新UI。我不明白为什么?那是ng的错误吗?

this.dialog
  .open(this.generateKeysDialogTemplate)
  .afterClosed()
  .subscribe((keyParams) => {
    if (!keyParams) {
      return;
    }
    this.request.password = this.request.password || '';
    this.request.privateKey = undefined;
    this.request.publicKey = undefined;
    this.request.fingerprint = undefined;
    this.changeDetectorRef.markForCheck();
    this.credentialService
      .generateKeys(this.request, keyParams.keyType, keyParams.keyLength)
      .pipe(first((x) => x !== undefined))
      .subscribe((credentialWithKey) => {
        const { credentialID, lastChangeUTC, ...properties } = credentialWithKey;
        setTimeout(() => {
          this.request = Object.assign({}, this.request, properties);
          this.changeDetectorRef.markForCheck();
        });
      });
  });

如您所见,我必须在模型中定义未定义的属性:

this.request.password = this.request.password || '';
this.request.privateKey = undefined;
this.request.publicKey = undefined;
this.request.fingerprint = undefined;

为了分配新值,使用它们更新UI。

this.request = Object.assign({}, this.request, properties);

我花了很长时间才完成这项工作,我真的不喜欢那个“修复”。

添加组件声明:

@Component({
  selector: 'mvp-credential-edit-form',
  templateUrl: './credential.detail.form.component.html',
})
export class CredentialDetailFormComponent extends FormViewBaseComponent {
  @Input()
  public request: Credential = {};

0 个答案:

没有答案