2016-05-30 78 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