I have a class with a field that is a JSON object and I need to display this JSON into a textarea. So I have create a set/get method to handle this beavior.
My Class:
class Element {
property: string;
value: string;
constructor(property: string,value: string){
this.property = property;
this.value = value;
}
}
class Model {
elements: Element[];
constructor(elements?: Element) {
this.elements = elements;
}
get elementsValue() {
return JSON.stringify(this.elements, null, 2);
}
set elementsValue(value: string) {
this.elements = JSON.parse(value);
}
}
My component:
@Component({
selector: 'my-app',
providers: [],
template: `
<div *ngFor="let model of models">
<textarea [(ngModel)]='model.elementsValue' rows="30" cols="120"></textarea>
</div> `,
directives: []
})
export class App {
models = [
new Model([new Element("test","value"),new Element("test","value")]),
new Model()
]
}
The elementsValue
value is not bound to the textarea. The textarea is empty
I have use this Plank as template.