Create states from array object in Angular ui-router - angularjs

I am able to create a state from an object like so...
var stateTest = {
name: '2',
views: {
'video': {
templateUrl: 'templates/2_video.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
};
$stateProvider.state(stateTest);
but what I need to do is create states from an array. This was my attempt and it is not working.
var stateList = [
{
name: '3',
views: {
'video': {
templateUrl: 'templates/3.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
},{
name: '4',
views: {
'video': {
templateUrl: 'templates/4.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
}
];
$stateProvider.state(stateList);
Then I thought I needed to try the foreach item approach...
var stateList = [
{
name: '3',
templateUrl: 'templates/3.html',
controller: 'VideoCtrl',
template: 'content'
},{
name: '4',
templateUrl: 'templates/4.html',
controller: 'VideoCtrl',
template: 'content'
},
];
stateList.forEach(function (item) {
VideoTest.stateProvider.state(item[0],{
views: {
'video': {
templateUrl: item[1],
controller: item[2]
},
'content': {
template: item[3],
controller: item[2]
}
}
});
});
Still no good :( Ideas!?
------- EDIT ------
As Craig pointed out in the answer, my array call was jacked up but I also didn't need to call the main module, but rather just place this in a module config like so:
VideoTest.config(function($stateProvider) {
var stateList = [
{
name: '3',
views: {
'video': {
templateUrl: 'templates/3.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
},{
name: '4',
views: {
'video': {
templateUrl: 'templates/4.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
}
];
stateList.forEach(function (item) {
$stateProvider.state(item.name,{
views: {
'video': {
templateUrl: item.views.video.templateUrl,
controller: item.views.video.controller
},
'content': {
template: item.views.content.template,
controller: item.views.content.controller
}
}
});
});
});

Your problem with your forEach implementation is indexing the item[0]. The item passed to the forEach callback (i.e. arg[0]) is the element itself so indexing this as an array doesn't make sense.
Your data:
var stateList = [
{
name: '3',
views: {
'video': {
templateUrl: 'templates/3.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
},{
name: '4',
views: {
'video': {
templateUrl: 'templates/4.html',
controller: 'VideoCtrl'
},
'content': {
template: '{{content}}',
controller: 'VideoCtrl'
}
}
}
];
and state setup:
stateList.forEach(function (item) {
VideoTest.stateProvider.state(item.name,{
views: {
'video': {
templateUrl: item.views.video.templateUrl,
controller: item.views.video.controller
},
'content': {
templateUrl: item.views.content.templateUrl,
controller: item.views.content.controller
}
}
});
});

Related

Angular ui-router $urlRouterProvider not working

function routeConfig($stateProvider, $urlRouterProvider) {
$stateProvider
.state('main', {
url: '/main',
templateUrl: 'app/pages/main/main.html',
title: 'Main',
sidebarMeta: {
icon: 'ion-android-home',
order: 0,
},
}).state('main.list', {
url: '/list',
templateUrl: 'app/pages/main/list/coinList.html',
title: 'Main',
controller: 'coinListCtrl',
resolve: {
coinMarketData: function(MarketCapService) {
return MarketCapService.getCrypto();
}
}
}).state('main.detail', {
url: '/detail/:symbol',
templateUrl: 'app/pages/main/detail/coinDetail.html',
title: 'Main',
controller: "coinDetailCtrl",
});
$urlRouterProvider.when('/main','/mail/list');
}
I have one main state and two child states as main.list and main.details. Using $urlRouterProvider service i want to go to main.list state by default. But it seems the command - $urlRouterProvider.when('/main','/mail/list');
is not working at all.
The code started working when i put $urlRouterProvider.when('/main','/mail/list'); above the registration of states, like this:-
function routeConfig($stateProvider, $urlRouterProvider) {
$urlRouterProvider.when('/main','/mail/list');
$stateProvider
.state('main', {
url: '/main',
templateUrl: 'app/pages/main/main.html',
title: 'Main',
sidebarMeta: {
icon: 'ion-android-home',
order: 0,
},
}).state('main.list', {
url: '/list',
templateUrl: 'app/pages/main/list/coinList.html',
title: 'Main',
controller: 'coinListCtrl',
resolve: {
coinMarketData: function(MarketCapService) {
return MarketCapService.getCrypto();
}
}
}).state('main.detail', {
url: '/detail/:symbol',
templateUrl: 'app/pages/main/detail/coinDetail.html',
title: 'Main',
controller: "coinDetailCtrl",
});
}
Though i would still like to know why? This might be due to lack of JS knowledge though, but still enlighten me..

ui-route works just the first time angularJS

i have a problems , after to have put the ui-route modal and page , i have another probleme my ui-route works just the first time and i don't know why
this is my route
angular.module('ui.bootstrap.demo').config(function($stateProvider, $urlRouterProvider,$qProvider){
$stateProvider
.state("connexion", {
url: "/",
views: {
// for column two, we'll define a separate controller
'Principal': {
abstract :true,
templateUrl: 'connexion.html'
}
}
})
.state('agenda', {
url: "/agenda",
views: {
// for column two, we'll define a separate controller
'Principal': {
abstract :true,
// templateUrl: 'agenda.html'
template:'<a ui-sref="view">Open me!</a>'
}
}
})
.state('modal', {
abstract: true,
parent: 'agenda',
url: '/modal',
onEnter: ['$uibModal', '$state', function($uibModal, $state) {
$uibModal.open({
animation: true,
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
}
})
}]
})
.state('view', {
url: ':id1',
parent: 'modal',
views: {
'modal#': {
template: '<div class="navbar">'+
'<div class="navbar-inner">'+
'<h4 class="brand">Quick Start</h4>'+
'<ul class="nav">'+
'<li><a ui-sref="foo">Route 1</a></li>'+
'<li><a ui-sref="bar">Route 2</a></li>'+
'</ul>'+
'</div>'+
'</div>'
}
}
})
.state('foo', {
url: ':id2',
parent: 'modal',
views: {
'modal#': {
template: '<h1>foo</h1><a ui-sref="view">back menu</a>'
}
}
})
.state('bar', {
url: ':id3',
parent: 'modal',
views: {
'modal#': {
template: '<h1>bar</h1><a ui-sref="view">back menu</a>'
}
}
})
$urlRouterProvider.otherwise("/");
$qProvider.errorOnUnhandledRejections(false);
})
and this a plunker,
http://plnkr.co/edit/4D6fsQv0FiPqtjoNiebS?p=preview
if some one can help me
thanks in advance
edit
Hello guys
re edit
.state('modal', {
abstract: true,
parent: 'agenda',
url: '/modal',
onEnter: ['$uibModal', '$state', function($uibModal, $state) {
$state.go('view');
$uibModal.open({
animation: true,
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
}
})
}]
})

Prevent reload controller in a nested views

I coding a modular adminpanel and only need load specific data one time.
<div id="main" ui-view>
<div ui-view="header"></div>
<div ui-view="sidebar"></div>
<!-- Start: Content-Wrapper -->
<section id="content_wrapper">
<div ui-view="topbar"></div>
<div ui-view="main"></div>
<div ui-view="footer"></div>
</section>
</div>
Topbar, header and footer should only load one time or an specific event.
I have set my states like this:
$stateProvider
.state('root', {
url: '/',
views: {
'header#': {
templateUrl: '/angular/apps/backend/views/template/header.html',
controller: 'AuthController'
},
'sidebar#':{
templateUrl: '/angular/apps/backend/views/template/sidebar.html',
controller: 'AppsController'
},
'topbar#':{
templateUrl: '/angular/apps/backend/views/template/topbar.html'
},
'footer#':{
templateUrl: '/angular/apps/backend/views/template/footer.html',
controller: ''
},
'main':{
templateUrl: '/angular/apps/backend/views/inicio.html',
controller: ''
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
.state('login', {
url: '/login',
templateUrl: '/angular/apps/backend/views/login.html',
controller: 'AuthController',
data: {
title: 'Iniciar Sesión',
bodyClass: 'external-page sb-l-c sb-r-c',
requiredLogin: false
}
})
.state('applist', {
url: '/:AppPermalink',
views: {
'header#': {
templateUrl: '/angular/apps/backend/views/template/header.html',
controller: 'AuthController'
},
'sidebar#':{
templateUrl: '/angular/apps/backend/views/template/sidebar.html',
controller: 'AppsController'
},
'topbar#':{
templateUrl: '/angular/apps/backend/views/template/topbar.html'
},
'footer#':{
templateUrl: '/angular/apps/backend/views/template/footer.html',
controller: ''
},
'main':{
templateUrl: '/angular/apps/backend/views/appresults.html',
controller: 'AppController'
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
.state('appedit', {
url: '/:AppPermalink/editar/:Itemid',
views: {
'header#': {
templateUrl: '/angular/apps/backend/views/template/header.html',
controller: 'AuthController'
},
'sidebar#':{
templateUrl: '/angular/apps/backend/views/template/sidebar.html',
controller: 'AppsController'
},
'topbar#':{
templateUrl: '/angular/apps/backend/views/template/topbar.html'
},
'footer#':{
templateUrl: '/angular/apps/backend/views/template/footer.html',
controller: ''
},
'main':{
templateUrl: function(stateParams){return '/angular/apps/backend/views/appedit_'+stateParams.AppPermalink+'.html'},
controller: 'AppController'
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
But when i change the state, the data of the sidebar has been reloaded.
I don't design a good nested views. How can doing this?
Solution found! I need use abstract state to define "sidebar", "header", "topbar" and "footer".
$stateProvider
.state('root', {
url: '',
abstract: true,
views: {
'header': {
templateUrl: '/angular/apps/backend/views/template/header.html',
controller: 'AuthController'
},
'sidebar':{
templateUrl: '/angular/apps/backend/views/template/sidebar.html',
controller: 'AppsController'
},
'topbar':{
templateUrl: '/angular/apps/backend/views/template/topbar.html'
},
'footer':{
templateUrl: '/angular/apps/backend/views/template/footer.html',
controller: ''
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
.state('root.home', {
url: '/',
views: {
'main#':{
templateUrl: '/angular/apps/backend/views/inicio.html',
controller: ''
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
.state('login', {
url: '/',
templateUrl: '/angular/apps/backend/views/login.html',
controller: 'AuthController',
data: {
title: 'Iniciar Sesión',
bodyClass: 'external-page sb-l-c sb-r-c',
requiredLogin: false
}
})
.state('applist', {
url: '/:AppPermalink',
parent: "root",
views: {
'main#':{
templateUrl: '/angular/apps/backend/views/appresults.html',
controller: 'AppController'
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
.state('appedit', {
url: '/:AppPermalink/editar/:Itemid',
parent: "root",
views: {
'main#':{
templateUrl: function(stateParams){return '/angular/apps/backend/views/appedit_'+stateParams.AppPermalink+'.html'},
controller: 'AppController'
}
},
data: {
title: 'Dashboard',
bodyClass: '',
requiredLogin: true
}
})
Controller only execute one time :)

Cannot load nested views' controllers with ocLazyload and ui-router

I have the code below:
.state('shops', {
abstract: true,
url: "/shops/:shopid",
data: {
pageTitle: 'Shop Settings'
},
views: {
'content#': {
templateUrl: "views/shops.html",
}
},
resolve: {
deps: ['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load({
name: 'MetronicApp',
insertBefore: '#ng_load_plugins_before',
files: [
'js/controllers/ShopDetailController.js',
'css/views/ngTable.css',
]
});
}]
}
})
.state('shops.feedbacks', {
url: "/feedbacks",
data: {
pageTitle: 'Feedbacks',
requireLogin: true
},
ncyBreadcrumb: {
label: 'Feedback Management'
},
views: {
'detail': {
templateUrl: "views/feedbacks.list.html",
controller: "FeedbackController"
}
},
resolve: {
deps: ['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load({
name: 'MetronicApp',
insertBefore: '#ng_load_plugins_before',
files: [
'js/controllers/FeedbackController.js',
]
});
}]
}
})
and the configuration is:
MetronicApp.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
debug: true
});
}]);
I am getting the error:
Error: [ng:areq] Argument 'FeedbackController' is not a function, got undefined
What did I do wrong? I also tried to inject FeedbackController in the parent state but it also doesn't work. Any idea?

Using $routeProvider with $stateProvider

in beginning I was just using $routeProvider as follows and it was giving me what I wanted.
angular.module('angularProject', ['angularProject.filters', 'angularProject.services', 'angularProject.directives', 'angularProject.controllers'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
title: 'Home',
templateUrl: 'partials/home.html',
controller: 'homeCtrl'
})
.otherwise({redirectTo: '/home'});
}]);
But when I also wanted to use $stateProvider as follows it is not giving me errors but also not allowing me to change state..
var myapp=angular.module('angularProject', ['ui.bootstrap','ui.router','angularProject.filters', 'angularProject.services', 'angularProject.directives', 'angularProject.controllers'])
myapp.config(['$routeProvider', '$stateProvider',function($routeProvider,$stateProvider) {
$routeProvider
.when('/home', {
title: 'Home',
templateUrl: 'views/home.html',
controller: 'homeCtrl'
})
.otherwise('/home');
$stateProvider
// .state('index', {
// url: "",
// views: {
// "viewA": {
// template: "index.viewA"
// },
// "viewB": {
// template: "index.viewB"
// }
// }
// })
.state('route1', {
url: "/route1",
views: {
"viewA": {
template: "route1.viewA"
},
"viewB": {
template: "route1.viewB"
}
}
})
.state('route2', {
url: "/route2",
views: {
"viewA": {
template: "route2.viewA"
},
"viewB": {
template: "route2.viewB"
}
}
})
// .otherwise({redirectTo: '/home'});
}]);
Any help on how to use $stateProvider with $routeProvider would be appreciated..
I don't think it's possible and I don't think that ui.router was built to be used with ngRoute.
According to the ui-router docs, you should use ui-router's own implementation of $routeProvider, called $urlRouterProvider.

Resources