【发布时间】:2017-09-12 22:11:52
【问题描述】:
Angular 1.6
我开发了一个dashboard application。已注册的仪表板组件有两个静态定义的 ui.router 状态:home-dashboard 和 other-dashboard。
现在我想根据仪表板数据动态定义ui.router 状态。为此,我在app.config 内循环。但是为了获取仪表板数据,StorageService 提供程序应该被注入到配置中。
收到的错误是:
Error: [$injector:unpr] Unknown provider: StorageService
如何注入提供者?有没有更好的方法来实现我的目标?
另外,我尝试将$stateProvider 移动到父控制器dashboardController。通过将其附加到app.config 内的应用程序,例如app.stateProvider = $stateProvider; 并导出将return default app; 放置在app.js 文件末尾的应用程序。
我得到的错误是'return' outside of function。
Providerservices/storage.service.js(是一个模拟API的类,以后会从DB获取数据):
class Storage {
constructor () {
this.dashboards = {
'home': {
id: '1',
name: 'Home',
view: 'home',
url: '/home',
component: 'homeDashboard',
widgets: [{
col: 0,
row: 0,
sizeY: 1,
sizeX: 1,
name: "Widget 1"
}, {
col: 2,
row: 1,
sizeY: 1,
sizeX: 1,
name: "Widget 2"
}]
},
'other': {
id: '2',
name: 'Other',
view: 'other',
url: '/other',
component: 'otherDashboard',
widgets: [{
col: 1,
row: 1,
sizeY: 1,
sizeX: 2,
name: "Other Widget 1"
}, {
col: 1,
row: 3,
sizeY: 1,
sizeX: 1,
name: "Other Widget 2"
}]
}
};
}
saveDashboards(dashboards) {
this.dashboards = dashboards;
}
listDashboards() {
return this.dashboards;
}
$get() {
return this.dashboards;
}
}
export { Storage };
app.js
import { DashboardCtrl } from './controllers/dashboardController';
import { homeDashboard } from './dashboards/home/homeDashboard.component';
import { otherDashboard } from './dashboards/other/otherDashboard.component';
import { aWidget } from './widgets/a_widget/aWidget.component';
import { Storage } from './services/storage.service.js';
import { Object2Array } from './filters/object2Array.js';
const app = angular.module('dashboardApp', [
'ui.router',
'ui.bootstrap',
'gridster'
])
.controller('DashboardCtrl', DashboardCtrl)
.component('aWidget', aWidget)
.component('homeDashboard', homeDashboard)
.component('otherDashboard', otherDashboard)
//.factory('StorageService', () => new Storage())
.provider('StorageService', Storage)
.filter('object2Array', Object2Array);
app.config(function ($urlRouterProvider, $stateProvider, StorageService) {
const dashboards = StorageService.listDashboards();
_.forEach(dashboards, function (d) {
$stateProvider.state({
name: d.view,
url: d.url,
component: d.component
});
});
/*
const homeState = {
name: 'home',
url: '/home',
component: 'homeDashboard'
};
const otherState = {
name: 'other',
url: '/other',
component: 'otherDashboard'
};
$stateProvider.state(homeState);
$stateProvider.state(otherState);
*/
$urlRouterProvider.otherwise('/home');
});
应用树:
../angular-dashboard/
├── LICENSE
├── README.md
├── dist
├── package.json
├── src
│ ├── app
│ │ ├── app.js
│ │ ├── controllers
│ │ │ └── dashboardController.js
│ │ ├── dashboards
│ │ │ ├── home
│ │ │ │ ├── homeDashboard.component.js
│ │ │ │ ├── homeDashboard.controller.js
│ │ │ │ └── templates
│ │ │ │ └── homeDashboard.template.html
│ │ │ └── other
│ │ │ ├── otherDashboard.component.js
│ │ │ ├── otherDashboard.controller.js
│ │ │ └── templates
│ │ │ └── otherDashboard.template.html
│ │ ├── filters
│ │ │ └── object2Array.js
│ │ ├── services
│ │ │ └── storage.service.js
│ │ └── widgets
│ │ └── a_widget
│ │ ├── aWidget.component.js
│ │ ├── aWidget.controller.js
│ │ ├── aWidget.settings.controller.js
│ │ └── templates
│ │ ├── aWidget.settings.template.html
│ │ └── aWidget.template.html
│ ├── index.html
│ └── style
│ ├── style-common.css
│ └── style.css
└── webpack.config.js
15 directories, 22 files
【问题讨论】:
-
与stackoverflow.com/questions/46150394/… 基本相同的问题。选择了错误的服务类型。
标签: javascript angularjs angular-ui-router