Vishal Kamal Vishal Kamal - 1 month ago 6
TypeScript Question

two way binding with addListener in angularjs 2

I'm not able to bind global variable.

here my code.

mxEvent.addListener(img, 'click',
mxUtils.bind(this, function(evt:any) {
this.enableRightSideBar = true;
console.log(this.enableRightSideBar);
}.bind(this))
)

console.log(this.enableRightSideBar, 'this.enableRightSideBar');


Show me "true", but it's not reflected on html.

Please help me.

Answer

There isn't much information in your question but I guess this is what you need to do:

mxEvent.addListener(img, 'click', (event:any) => { this.enableRightSideBar = true; })

Update

Seems this callback is fired outside angular zone so you can use ChangeDetectorRef to refresh view:

constructor(private cdRef: ChangeDetectorRef) {}

...
this.enableRightSideBar = true; 
this.cdRef.detectChanges();