我正在開發的應用程序Angular2動態插入,並且我面臨一個問題:Angular2:雙向數據上組件綁定使用DynamicComponentLoader
我有一組可以使用UI來選擇不同的對象。每個對象都有一組選項(對不同的對象不同),可以使用UI進行編輯。現在,我使用DynamicComponentLoader爲當前選定的對象插入特定組件,以便它可以正確處理其選項。 問題是我不知道如何將當前選定對象的數據綁定到動態插入的選項組件。
@Component({
selector: 'dynamic',
template: `<div>Options:</div>
<div>Property1: <input type="number" /></div>
<div>Property2: <input type="text" /></div>`
// template: `<div>Options:</div>
// <div>Property1: <input type="number" [(ng-model)]="currentSelection.property1" /></div>
// <div>Property2: <input type="text" [(ng-model)]="currentSelection.property1" /></div>`
})
class DynamicComponent {
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Selected: {{currentSelection.name}}!</h2>
<div #container></div>
</div>
`
})
class App {
currentSelection = {name: 'Selection1', property1: 10, property2: 'test'};
constructor(private loader: DynamicComponentLoader, private elementRef: ElementRef) {
loader.loadIntoLocation(DynamicComponent, elementRef, 'container');
}
}
Here是plunker幫助你理解我的問題:
是身邊有唯一的例子是這樣的https://github.com/angular/angular/blob/1aeafd31bd440a4997362e66738926387940fc1e/modules/angular2_material/src/components/dialog/dialog.ts –