简体   繁体   English

Angular 4属性绑定刷新

[英]Angular 4 property binding refreshing

i have created one JSON viewer and editor using Angular4- JsonEditor . 我已经使用Angular4- JsonEditor创建了一个JSON查看器和编辑器。 Application loading time its reading a local json file and populating to view. 应用程序加载时会读取本地json文件并填充以供查看。 And its working fine 而且工作正常

Now i have one input box and btn. 现在我有一个输入框和btn。 i will enter webservice url in the input box and get the json data as a response. 我将在输入框中输入webservice网址,并获取json数据作为响应。 On click of the button am getting the response as expected but the problem is my view is not getting changed. 单击该按钮时,正在获得预期的响应,但问题是我的视图没有改变。

To populate the data i have used [data]="payLoadData" in component once i get the service response am setting the jsonData but still changes not reflecting in view. 为了填充数据,一旦获得服务响应,就在组件中使用[data] =“ payLoadData”设置jsonData,但更改仍未在视图中反映出来。

  ngOnInit() { this.editorOptions.modes = ['code', 'text', 'tree', 'view']; // set all allowed modes this.payLoadData = this.jsondata[0]; // initial file read data } submitSequence(){ this._responseService.getPayLoadFromUrl() .subscribe(data=>{ this.payLoadData = data; }, error=>{ console.error(error); }) } 
 <div class="inputSection"> <input type="text" placeholder="Enter sequence url"> <button (click)="submitSequence()">Submit</button> <button (click)="generateResponse()">Generate response.json</button> </div> <json-editor [options]="editorOptions" [data]="payLoadData"></json-editor> 

I guess if you feel inclined to do so, you could edit the package source to include onChanges . 我想如果您愿意,可以编辑包源以包含onChanges For example: 例如:

import { OnChanges, SimpleChanges } from '@angular/core';
...
export class JsonEditorComponent implements OnInit, OnChanges {
  ...
  ngOnChanges(changes: SimpleChanges): void {
   if(changes.data.firstChange) return;
   this.destroy();
   this.editor = new editor(this.rootElement.nativeElement, this.options, changes.data.currentValue);
  } 
  ...
}

This should, in theory, update the property binding when payLoadData changes. 从理论上讲,这应该在payLoadData更改时更新属性绑定。 (unfortunately I have no way of testing this right now.) (不幸的是,我现在无法对此进行测试。)

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

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