Angular 2: DynamicComponentLoader data not binding

2019-09-05 15:25发布

问题:

I am using the DynamicComponentLoader as detailed here on the angular2 API Guide,

https://angular.io/docs/ts/latest/api/core/DynamicComponentLoader-class.html

I have setup my code to look like the following:

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);
    }
}

I see the template in my web browser however the {{item}} doesn't bind in the template.

Im not too sure where i am going wrong and any help would be appreciated.

Thanks,

回答1:

There is a known bug related to the use of loadAsRoot. The suggested workaround is to use loadNextToLocation or loadIntoLocation.

See this answer: Bindings not working in dynamically loaded component

Hope it helps you, Thierry