0

我已经通过以下方式实现了一个组件

angular.module('moduleName')
        .component('myComponent', {
            templateUrl: 'templatePath1.tpl.html',
            controller: myController,
            controllerAs: 'vm',
            bindings: {
                b1: '&',
                b2: '&'
            }
        });

我用它作为<my-component b1="someThing1" b2="someThing2"></my-component>

现在,我想将其myController与位于templatePath2.tpl.html.

一种方法是创建另一个组件myComponent2

angular.module('moduleName')
        .component('myComponent2', {
            templateUrl: 'templatePath2.tpl.html',
            controller: myController,
            controllerAs: 'vm',
            bindings: {
                b1: '&',
                b2: '&'
            }
        });

有什么方法可以使用以前的组件并根据 attr 选择 templateUrl?如果是,应该怎么做?

4

1 回答 1

0

templateUrl可以是一个函数,它获取元素和属性作为参数:

angular.module('moduleName')
    .component('myComponent', {
        templateUrl: function(element, attrs) {
                      return 'templatePath' + attrs.x + '.tpl.html';
                     },

<my-component x="1"></my-component>
于 2016-11-10T14:04:40.517 回答