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

1 つの方法は、別のコンポーネントを作成することです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 に答える