ngx-json-viewer icon indicating copy to clipboard operation
ngx-json-viewer copied to clipboard

Dynamic value?

Open voznik opened this issue 7 years ago • 11 comments

Is it possible to update the json-formatted view when an object changes dynamically?

I like your component, and wanted to use it to display reactive form state/data, but your current implementation is useless for that.

voznik avatar Feb 25 '18 18:02 voznik

please check out v2.2.0 to see if it works for you.

hivivo avatar Feb 25 '18 23:02 hivivo

no, it doesn't work with v2.2.0

I will make stackblitz example for this issue later

voznik avatar Feb 28 '18 10:02 voznik

Hi. I'm using v2.3.1 but still not able to update viewer after model change. I can see model change but viewer not updating itself dynamically. I've tried to follow stackblitz demo but I couldn't make it work.

I've a component and 'data' model updates when user interact with form. I can see model change with {{data | json}} but viewer doesn't update itself.

Custom components HTML {{ data | json }} <br> <ngx-json-viewer [json]="data"></ngx-json-viewer> Custom components TS export class ScpDebuggerComponent { @ Input('data') data: any; constructor() { } } SAMPLE USAGE <m-scp-debugger [(data)]="JSON_DATA"></m-scp-debugger>

sezer avatar Oct 04 '18 11:10 sezer

Yes able to update. you are to re-initialize the data fully every-time not just part

roastedmonk avatar Feb 16 '19 23:02 roastedmonk

I'm experiencing the same issue with v2.4.0. I think there is a missing handler to detect json changes and force reloading the json within the viewer. This is definitely a bug.

patrickrioux avatar Oct 03 '19 22:10 patrickrioux

Its works with a function:

Html <ngx-json-viewer [json]="getJson()"></ngx-json-viewer>

Typescript getJson(){ return JSON.parse(JSON.stringify(this.json) || '') }

javierarocena avatar Oct 10 '19 20:10 javierarocena

Yes that method works but it is super slow in comparison to standard Angular binding {{ model | json }}. When changing the model (like typing in a textarea) the text is so slow that is ngx-json-viewer is unusable.

patrickrioux avatar Oct 10 '19 22:10 patrickrioux

Same issue here. Setting a property on the object didn't refresh the JSON view, so I set object itself to something else (null, {}, etc) then add back the properties with updated values. You could probably accomplish this same thing using a deep copy and storing the old values, setting the JSON object to null, then reassigning the old values (with updates) to the JSON object.

// Using the `cloneDeep` function from `lodash`
const oldObject = cloneDeep(jsonObject)
jsonObject = null

oldObject.someProperty = "newValue"

jsonObject = oldObject

Won't work for everyone, but at least it's better than doing a function (which will be called repeatedly by Angular).

kbpontius avatar Nov 21 '19 21:11 kbpontius

You can use this cheat to fix problem: <ngx-json-viewer *ngIf="flag" [json]="obj" [expanded]="true"> <ngx-json-viewer *ngIf="!flag" [json]="obj" [expanded]="true">

Set 2 tag ngx-json-viewer with a flag, when "obj" change, you toggle flag too, it will update new obj.

It works for me

donhieu123 avatar Nov 27 '19 02:11 donhieu123

It works if you use ts ... operator for example: submit() { if (this.form.valid) { this.validUser = { ...this.model}; }

<ngx-json-viewer [json]="validUser"></ngx-json-viewer>

Where model is being used by the form, you can use any other value instead of this.model.

JamilOmar avatar Feb 12 '20 15:02 JamilOmar

angular 💩

Iuriy-Budnikov avatar Oct 26 '22 15:10 Iuriy-Budnikov