简体   繁体   English

更改后角度8值将不会在视图上更新

[英]Angular 8 value won't update on view after change

I have a small component in Angular with a method that (for now) sets a timeout and changes the value of a variable. 我在Angular中有一个小的组件,该组件具有(现在)设置超时并更改变量值的方法。

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

@Component({
  selector: 'my-component',
  templateUrl: './my-view.html',
  styleUrls: ['./my-view.scss'],
  changeDetection: ChangeDetectionStrategy.Default
})
export class MyComponent {

  status: boolean = false;

  changeStatus(): void {

    setTimeout(() => {
      this.status = true;
    }, 1500);
  }
}

And the HTML 和HTML

<div>
  <form #myForm="ngForm">
    <input name="code" type="text" class="form-control" [(ngModel)]="code" #codeInput="ngModel" required placeholder="Enter your code" />
  </form>
</div>

<div class="row">
  <div class="col-md-5" (click)="changeStatus()">
    <mat-icon aria-label="clear-all" *ngIf="!status">&#xe042;</mat-icon>
    <a>Change status</a>
  </div>
  <div class="col-md-7">
    <button type="button" class="btn-flat app-btn-flat">Cancel</button>
    <button type="button" class="btn app-btn" [disabled]="myForm.invalid || myForm.pristine">Save</button>
  </div>
</div>

If I log the value of 'status' in the component. 如果我在组件中记录“状态”的值。 I get the new value of 'true' but it won't change on the view unless I focus the cursor on the input and then click anywhere outside it. 我得到了'true'的新值,但除非将光标放在输入上,然后单击其外部的任何位置,否则它不会在视图上改变。

Why is that happening? 为什么会这样呢? how can I solve it? 我该如何解决?

You're doing it outside of the normal change detection cycle. 您正在正常更改检测周期之外执行此操作。 This will work: 这将起作用:

export class MyComponent {

  status: boolean = false;

  constructor(private _cdr: ChangeDetectorRef) { }

  changeStatus(): void {

  setTimeout(() => {
    this.status = true;
    this._cdr.detectChanges()
  }, 1500);
 }
}

EDIT : It's updating on your next interaction with the UI because angular is running it's change detection at that point in time and it catches the change in your component and updates the UI. 编辑 :它正在更新您与UI的下一次交互,因为angular正在运行,它在该时间点进行更改检测,并且它捕获了组件中的更改并更新了UI。 If you're doing things asynchronously (unless you're using angular APIs, like HttpService), then you've got to tell the ChangeDetector that there are changes manually. 如果您是异步进行的(除非您使用的是Angular API,例如HttpService),那么您必须告诉ChangeDetector手动进行了更改。

Why is that happening? 为什么会这样呢?

You set changeDetection to OnPush in one of the ancestor component. 您在祖先组件之一中将changeDetection设置为OnPush That property cannot be overridden. 该属性不能被覆盖。 Ref: https://angular.io/api/core/ChangeDetectionStrategy 参考: https : //angular.io/api/core/ChangeDetectionStrategy

how can I solve it? 我该如何解决?

Unset changeDetection in the ancestor component, or manually detect changes like Adam's answer. 取消设置祖先组件中的changeDetection ,或手动检测诸如Adam的答案之类的变化。

constructor(private _cdr: ChangeDetectorRef) {}

changeStatus(): void {
  setTimeout(() => {
    this.status = true;
    this._cdr.detectChanges()
  }, 1500);
}

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

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