2016-05-30 48 views
0

我很難嘗試使用Angular實現動態加載的內容...我有幾個小部件,他們的partialName變量通過綁定加載後,請求。這就是爲什麼我在下面使用scope.$watch。即使模板設置了正確的內容,視圖本身也不會刷新以顯示適當的內容。我嘗試了一些像下面的$broadcast一樣的東西,但沒有運氣。我怎樣才能做到這一點?謝謝!如何使用AngularJS加載動態指令內容?

export class CioTemplateLoader implements ng.IDirective { 
    public template: string; 
    public partials: { [id: string]: string; } = {}; 
    public scope = { 
    partialName: '=' 
    }; 
    public route: any; 

    static $inject = [ 
    '$route' 
    ]; 

    constructor(){//$route: any) { 
    // this.route = $route; 
    this.partials['claimOverview'] = require('../../claims/claim-overview.html'); 
    //... 
    } 

    link = (scope: ng.IScope, elem: JQuery, attributes: ng.IAttributes) => { 
    var that = this; 
    scope.$watch('partialName', function (value: string) { 
     if (value) { 
     that.template = that.partials[value]; 
     debugger; 
     scope.$broadcast("REFRESH"); 
     } 
    }); 
    } 

    public static Factory(): angular.IDirectiveFactory { 
    var directive =() => { 
     return new CioTemplateLoader(); 
    }; 

    return directive; 
    } 
} 

enter image description here

回答

1

你想要做的是編譯動態HTML模板,並將其呈現,對不對?如果是這樣,這個問題可能會被標記爲重複,例如Compiling dynamic HTML strings from database

+0

謝謝,我要檢查並找回你。 – eestein

+0

你是對的,那是答案,這可以被標記爲重複[我剛剛那樣做]。謝謝! – eestein