【问题标题】:How to load dynamic directive content using AngularJS? [duplicate]如何使用 AngularJS 加载动态指令内容? [复制]
【发布时间】:2016-09-28 07:37:12
【问题描述】:

我很难尝试使用 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;
  }
}

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我会检查一下并回复你。
    • 你是对的,这就是答案,这可以被标记为重复[我刚刚这样做了]。谢谢!
    猜你喜欢
    • 2013-12-18
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多