Angular 1.6
I develop a dashboard application. There are two statically defined ui.router
states for the registered dashboard components: home-dashboard
and other-dashboard
.
Now I want to define ui.router
states dynamically based on dashboards data. For this, I do a loop inside app.config
. But in order to get dashboards data, StorageService
provider should be injected into the config.
The error received is:
Error: [$injector:unpr] Unknown provider: StorageService
How to inject provider? Is there a better way to achieve my goal?
Also, I tried to move $stateProvider
into dashboardController
, the parent controller. By attaching it to app inside app.config
, like app.stateProvider = $stateProvider;
and exporting the app placing return default app;
in the end of app.js
file.
The error I got was 'return' outside of function
.
Provider services/storage.service.js
(it is a class which simulates API, in the future it will get data from 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');
});
App tree:
../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