簡體   English   中英

即使在更改@Input boolean 的值后,NgOnChanges 也不會觸發?

[英]NgOnChanges not firing even after changing value of @Input boolean?

我是 angular 的新手,已經看到了各種答案,但無法解決。我正在使用 Angular 11。這是我的代碼:

    @Component({
      selector: 'app-custom-quill-editor',
      templateUrl: './custom-quill-editor.component.html',
      styleUrls: ['./custom-quill-editor.component.css'],
    })
    export class CustomQuillEditorComponent implements OnInit, OnChanges {
      static textChange() {
        throw new Error('Method not implemented.');
      } 
      quill1: any;
       data: any[] = [new Temp()];
       @Input() flag:boolean=false;
      constructor(private getDataService: GetDataService) {   
      }
    ngOnChanges(changes:SimpleChanges){
    console.log("In ng changes");
 console.log(changes);
let tdelta = {
      ops: this.data,
    };
    var Delta = Quill.import('delta');
    //let editor = new Quill('#editor');  // finding quill editor with id editor
    const delta = new Delta(tdelta);
    //editor.setContents( delta ,'api');
    var quill = new Quill('#editor-container', {
      modules: {
        toolbar: [
          ['bold', 'italic', 'underline'],
          ['blockquote'],
          [{ header: 1 }, { header: 2 }],
          [{ list: 'ordered' }, { list: 'bullet' }],
        ],
      },
      placeholder: 'Compose an epic...',
      theme: 'snow', // or 'bubble'
    });
    this.quill1 = quill;
    this.quill1.updateContents(delta, 'api');
    //trigerring these events if text is changed in editor
    quill.on(
      'text-change',
      this.textChange
    );
  }
    }
 ngOnInit(){
    this.getDataService.getJSON().subscribe((fetchedData) => {
      console.log('Fetched data in component');
      console.log(fetchedData);
      console.log("data before assigning it to fetchedData");
      console.log(this.data);
      this.data=fetchedData;
      console.log(this.flag);
      this.flag=!this.flag;
      console.log(this.flag);
      //this.cd.detectChanges();

      console.log("data after assigning it to fetchedData");
      console.log(this.data);
      console.log('Final data array');
      console.log(this.data);

    });
  }

在控制台中,我可以看到變量標志的值發生了變化,但仍然沒有觸發 ngOnChnages。 有沒有其他方法可以做到這一點?

為什么不在輸入中使用設置器?

_private flag
@Input set flag(value){
   this._flag=value;
  ...do something...
}

get flag()
{
   return this._flah
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM