Is there a way to dynamically render different templates for an angular 1.5 component

后端 未结 3 746
孤城傲影
孤城傲影 2021-02-02 15:04

I have a number of angular 1.5 components that all take the same attributes and data structure. I think they could be re-factored into a single component, but I need a way to dy

3条回答
  •  半阙折子戏
    2021-02-02 15:46

    This is not something that components were specially made for. The task narrows down to using a directive with dynamic templates. The existing one is ng-include.

    To use it within a component, it should be:

    var myComponentDef = {
      bindings: {
        type: '<'
      },
      template: '
    ', controller: function () { this.$onChanges = (changes) => { if (changes.type && this.type) { this.templateUrl = this.type + '.html'; } } } }

提交回复
热议问题