我正在使用DynamicComponentLoader,详见angular2 API指南
https://angular.io/docs/ts/latest/api/core/DynamicComponentLoader-class.html
我已将代码设置为如下所示:
import {Page} from 'ionic-framework/ionic';
import { Component, View, DynamicComponentLoader, Injector } from 'angular2/core';
import { RouteParams } from 'angular2/router';
import { DataService } from '../../services/dataService';
import { Section } from '../../models/section';
@Component ({
selector : 'itemView',
template : '<div id="content"></div>'
})
export class ItemView {
constructor (private dataService : DataService, private _routeParams : RouteParams, dcl: DynamicComponentLoader, injector: Injector) {
var sectionid = this._routeParams.get ('sectionid');
var categoryid = this._routeParams.get ('categoryid');
var itemid = this._routeParams.get ('itemid');
var views = {product: ItemproductView, dispensor: ItemdispensorView, signage: ItemsignageView, equipment: ItemequipmentView};
if (categoryid !== null) {
this.item = dataService.getCategoryItem (sectionid, categoryid, itemid);
} else {
this.item = dataService.getItem (sectionid, itemid);
}
dcl.loadAsRoot(views[sectionid], '#content', injector);
}
}
/* ****************** */
// DYNAMIC VIEWS //
/* ****************** */
@Component ({
selector : 'itemproductView',
templateUrl : 'build/components/item/item-product.html'
})
export class ItemproductView {
constructor(private dataService : DataService, private _routeParams : RouteParams) {
var sectionid = this._routeParams.get ('sectionid');
var categoryid = this._routeParams.get ('categoryid');
var itemid = this._routeParams.get ('itemid');
this.item = dataService.getCategoryItem (sectionid, categoryid, itemid);
}
}
@Component ({
selector : 'itemdispensorView',
templateUrl : 'build/components/item/item-dispensor.html'
})
export class ItemdispensorView {
constructor(private dataService : DataService, private _routeParams : RouteParams) {
var sectionid = this._routeParams.get ('sectionid');
var itemid = this._routeParams.get ('itemid');
this.item = dataService.getItem (sectionid, itemid);
}
}
@Component ({
selector : 'itemsignageView',
templateUrl : 'build/components/item/item-signage.html'
})
export class ItemsignageView {
constructor(private dataService : DataService, private _routeParams : RouteParams) {
var sectionid = this._routeParams.get ('sectionid');
var itemid = this._routeParams.get ('itemid');
this.item = dataService.getItem (sectionid, itemid);
}
}
@Component ({
selector : 'itemequipmentView',
templateUrl : 'build/components/item/item-equipment.html'
})
export class ItemequipmentView {
constructor(private dataService : DataService, private _routeParams : RouteParams) {
var sectionid = this._routeParams.get ('sectionid');
var itemid = this._routeParams.get ('itemid');
this.item = dataService.getItem (sectionid, itemid);
}
}
我在网络浏览器中看到了模板,但{{item}}未在模板中绑定。
我不太确定我哪里出错了,任何帮助都会受到赞赏。
谢谢,
答案 0 :(得分:2)
有一个与使用loadAsRoot
相关的已知错误。建议的解决方法是使用loadNextToLocation
或loadIntoLocation
。
请参阅此答案:Bindings not working in dynamically loaded component
希望它可以帮到你, 亨利