我正在尝试使用Angular 1.5.8构建一种动态仪表板.直到最后的障碍,我取得了不错的进步.这实际上是渲染动态组件.
我已经尝试了2个选项,要么添加一个ui-view并以编程方式传递窗口小部件的名称,要么,这就是我猜的路径更正确,我需要弄清楚如何渲染动态窗口小部件.
例如:当我将item和item附加到dashItems集合时,它应该呈现一个新的小部件(基于我提供的名称)
我已经看到我可以使用替换模板ngInclude,但我仍然不清楚如何动态注入模板和控制器.(我所有的模板都不会共享一个共同的控制器).
JavaScript的:
angular
.module('myDashboard', [])
.config(routesConfig)
.component('dashboard', {
templateUrl: 'dashboard/dashboard.tpl.html',
controller: dashboardController
})
.component('widgetPie', {
template: '<h3>Pie Graph</h3>',
controller: function($log) {
$log.info('widgetPie: loaded');
}
})
.component('widgetLine', {
template: '<h3>Line Graph</h3>',
controller: function($log) {
$log.info('WidgetLine: loaded');
}
});
function routesConfig($stateProvider) {
// this is only needed if I go the ui-view route.. I assume
$stateProvider
.state('widgetPie', { component: 'widgetPie'})
.state('widgetLine', { component: 'widgetLine'});
}
function dashboardController($log) …Run Code Online (Sandbox Code Playgroud) 我可以在AngularJS 1.5组件中使用多个模板吗?我有一个组件有一个属性,所以我想根据该属性名称加载不同的模板.如何根据元素的属性名称加载模板?
jsConfigApp.component('show', {
templateUrl: 'component/show.html', //How to change it based on attribute value?
bindings:{
view:"@"
},
controller: function () {
console.log(this.view)
if (this.view = "user") {
console.log("user")
} else if (this.view = "user") {
console.log("shop")
} else {
console.log("none")
}
}
})
Run Code Online (Sandbox Code Playgroud)
谢谢.