In Angular2 I am creating a modal window which needs to be toggled on and off. During the development I stumbled upon something weird:
When I change the activate variable inside my Modal class with an external button. The view is not updating.
Printing the activate variable in the console goes without any problem. I see the variable toggle between true and false.
It looks like I am missing code which forces the view to render with the new values.
Does anyone have an idea what I might be missing here?
The code below in a Plunkr
app.ts
import {Component} from 'angular2/core';
import {Modal} from './modal';
@Component({
selector: 'my-app',
providers: [Modal],
template: `
<div>
<button (click)="toggle()">toggle</button>
<button (click)="showActivate()">print</button>
<modal></modal>
</div>
`,
directives: [Modal]
})
export class App {
constructor(private modal: Modal) {
}
showActivate() {
this.modal.showActivate();
}
toggle() {
this.modal.toggleActivate();
}
}
modal.ts
import {Component, Injectable} from 'angular2/core'
@Component({
selector: 'modal',
template: `
<div>
<h2>Modal: {{activate}}</h2>
</div>
`,
directives: []
})
@Injectable()
export class Modal {
activate: true;
constructor() {
this.activate = false;
}
showActivate() {
console.log('Modal:showActivate', this.activate);
}
toggleActivate() {
console.log('Modal:toggleActivate', this.activate);
this.activate = !this.activate;
}
}