Changing a component variable is not updated until the mouse appears in Angular 2

After upgrading from angular2 -alpha to the latest version, changes to the boolean value are not updated * ngIf until certain actions are performed.

Here is this component:

declare var CKEDITOR: any;
export class FieldComponent {

 @Input() field: any = {};
 ckeditor: any;
 editable: boolean = false;

 constructor(){

  this.switchToUnEditable();

  this.listenForEvent("FieldEditableEvent", (data) => {
    this.switchToEditable();
  });

 }

 switchToEditable(){
  this.ckeditor.destroy();
  this.ckeditor = CKEDITOR.replace(this.field.id);
  this.editable = true;
 }

switchToUnEditable() {
  if(this.ckeditor) {
   this.ckeditor.destroy();
  }
  this.ckeditor = CKEDITOR.replace(this.field.id, {
   toolbar: []
  })
  this.editable = false;
 }



}

And here is the template for this component:

<div *ngIf="editable" class="green-background white-text unlocked">
  This field is unlocked!
  <button (click)="switchToUnEditable()"> 
   Save and close. 
  </button>
</div>
<div *ngIf="!editable" class="red-background white-text locked">
  This field is locked!
</div>
<textarea [attr.name]="field.id" [(ngModel)]="field.value">  
</textarea>

This was used to work fine before the update - now I have to hover over the button inside the div to get * ngIf, which will be applied in the view.

Alternatively, I can execute the event further down the component tree, in which case * ngIf will be applied. For example, opening side-nav changes the view from locked to unlocked.

Angular 2 - , , , , .

+4
1

, switchToEditable() switchToUnEditable() - Angulars.

, :

constructor(private cdRef:ChangeDetectorRef) {}

switchToEditable(){
  this.editable = true;
  this.cdRef.detectChanges();
}

switchToUnEditable() {
  ...
  this.editable = false;
  this.cdRef.detectChanges();
}
+8

Source: https://habr.com/ru/post/1657614/


All Articles