如何使用AngularJS加载动态指令内容?

12 浏览
0 Comments

如何使用AngularJS加载动态指令内容?

我在尝试使用Angular实现动态加载内容时遇到了一些困难... 我有几个小部件,它们的partialName变量是通过绑定加载的,在请求之后。这就是为什么我在下面使用了scope.$watch。尽管模板已经设置了正确的内容,但视图本身并没有刷新以显示适当的内容。我尝试过像下面的$broadcast之类的东西,但没有成功。我该如何实现这个?谢谢!

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

0
0 Comments

如何使用AngularJS加载动态指令内容?

问题的原因:想要做的是动态编译HTML模板并渲染它们,对吗?如果是这样的话,这个问题可能会被标记为重复,比如Compiling dynamic HTML strings from database

解决方法:我会去查看并回复你。

你是正确的,那就是答案,这个问题可以被标记为重复[我刚刚做了这个]。谢谢!

0