嗨我有一種情況,我想更改組件內部屬性的值。以下是我迄今爲止:更改子組件中屬性的值不起作用
import {Component, OnInit} from 'angular2/core';
import {TinymceComponent} from './../../../common/components/tinymce.component';
@Component({
selector: 'templates',
directives: [TinymceComponent],
template: `<button (click)="update()">Update</button>
<tinymce [(description)]="product.description"></tinymce>`
})
export class ProductEditComponent implements OnInit {
public product: any;
public ngOnInit() {
this.product = {
description: 'hello world'
}
}
public update(){
console.log(this.product)
}
}
這是我TinyMceComponent:
import {Component, OnInit, Input} from 'angular2/core';
declare var tinymce: any;
@Component({
selector: 'tinymce',
template: '<textarea id="tinymce-editor"></textarea>',
})
export class TinymceComponent implements OnInit {
@Input() description: string;
public ngOnInit() {
setTimeout(() => this.description = 'new text', 5000)
}
}
什麼此刻正在發生的事情是product.description的值保持爲「世界你好」後,即使setTimeout函數得到執行並將描述設置爲'new text'
我想要做的是能夠從TinyMceComponent中的產品模型中更改description屬性。現在我不確定我的方法是否正確我認爲它會起作用,因爲像這樣的東西曾經在角1中工作,但我可能是錯的。
誰能告訴我怎麼做到這一點?
您沒有使用TinymceComponent的模板中的描述。 –