In Angular 1,我們可以使用templateUrl來動態加載不同的外部模板,如下所示。如何在Angular 2組件中動態加載外部模板?
angular.module('testmodule).diretive('testDirective'), function(){
return {
restrict: 'EA',
replace: true,
scope: {
data: '@',
},
templateUrl: function(element, attrs) {
if(attrs.hasOwnProperty("attr")){
return "views/test1.html";
} else {
return "views/test2.html";
}
}
}
我的問題是如何實現下角2成分相同的功能?
@Component({
selector: 'testDirective, [testDirective]',
template: require('./views/test1.html') or require ('./views/test2.html')
})
export class Angular2Component {
...
}
的https: //stackoverflow.com/questions/38888008/how-can-i-use-create-dynamic-template-to-compile-dynamic-component-with -angular –
可能的重複[如何使用/創建動態模板來編譯動態組件與Angular 2.0?](https://stackoverflow.com/questions/38888008/how-can-i-use-create-dynamic-template -to-compile-dynamic-component-with-angular) –