UI-Router parent controller doesn't initiate - angularjs

I'm using AngularJs UI-Router for my app, but I'm with a problem where the parent's controller isn't initiated.
This is my state structure:
.state('main', {
abstract: true,
controller: 'MainController',
controllerAs: 'vm',
resolve: {
config: function($timeout){
return $timeout(function() {
return console.log('loaded')
}, 1000);
}
}
})
.state('home', {
parent: 'main',
url: '/Home',
views: {
'content#': {
templateUrl: 'view/home.html'
}
}
})
.state('contact', {
parent: 'main',
url: '/Contact',
views: {
'content#': {
templateUrl: 'view/contact.html',
}
}
})
The template home.html and contact.html are displaying on the view just fine. But inside the MainController I have just a console.log but it doesn't appear on the console.
If I make some changes, I can make it work. This is the working example:
.state('main', {
abstract: true,
views: {
'main': {
template: '<div ui-view="content"></div>',
controller: 'MainController',
controllerAs: 'vm'
}
}
[...code...]
.state('home', {
parent: 'main',
url: '/Home',
views: {
'content': {
[...code...]
This way, everything works as expected, The view appear and the console from the controller also appear.
But it doesn't seem "right" because I need to create a template just to hold the child states.
Is there a way to make it work with the first option?

Well, to answer:
... Is there a way to make it work with the first option?
Have to say: NO. The point is:
Scope Inheritance by View Hierarchy Only
Keep in mind that scope properties only inherit down the state chain if the views of your states are nested. Inheritance of scope
properties has nothing to do with the nesting of your states and
everything to do with the nesting of your views (templates).
It is entirely possible that you have nested states whose templates populate ui-views at various non-nested locations within
your site. In this scenario you cannot expect to access the scope
variables of parent state views within the views of children states.
So, what happened is - the child state views: {} definition:
.state('contact', {
parent: 'main',
views: {
'content#': {
...
... forced child to skip parent view. Not parent state. It skips a parent view. There is no parent view, from which it could inherit the $scope.
The view of a child state 'contact', is injected directly into root (index.html) ui-view="content", it will not trigger parent view...
So, use the second approach, which is absolutely correct, to achieve what is exepected
Check also these for farther details and working examples:
How to inherit resolve data in ui-router
Nested states or views for layout with leftbar in ui-router?

Related

AngularJS $stateProvider - change parent template view from child state

Hello everyone this is my first post in Stackoverflow!
I am new to $stateProvider and trying to complete the following behavior:
I am trying to modify and load a new state's parent view from its child state. There is the main view (parent) that has two views, a content view and a navigation menu view on the left hand side. There are multiple child states for a parent state and once the user goes to a child state I would like to load a different navigation menu that was initialized in the parent state.
.state("parent", {
views: {
'contentView': {
templateUrl: baseUrl + "home.html",
controller: "homeController"
},
'navBar': {
templateUrl: baseUrl + "navMenus/navMenu1.html",
controller: "navMenuController"
}
}
})
.state("parent.child", {
views: {
'navBar': {
templateUrl: baseUrl + "navMenus/navMenu2.html",
controller: "navMenuController"
}
}
You can use Absolute Name. In your case, try navBar#parent.

AngularJS. Templates are not included in ui-views, when transitioning between child states of different abstract parents

Templates are not included in ui-views, when transitioning between child states of different abstract parents.
Here is the plunker and link to the editor. Navigate form login (auth.login) state to dashboard (app.dashboard) state, header and footer templates are not included in ui-views.
Example of $stateProvider:
.state('auth', {
abstract: true,
views: {
'app-outer': {
templateUrl: 'auth.html'
}
}
})
.state('auth.login', {
url: '/login',
views: {
'app-inner': {
templateUrl: 'login.html'
}
}
})
.state('app', {
abstract: true,
views: {
'header': {
templateUrl: 'header.html'
},
'app-outer': {
templateUrl: 'app.html'
},
'footer': {
templateUrl: 'footer.html'
}
}
})
.state('app.dashboard', {
url: '/dashboard',
views: {
'app-inner': {
templateUrl: 'dashboard.html'
}
}
});
I've tried passing {reload: true} in the ui-sref-opts and used $state.go() method with {reload: true}.
As a temporary solution I've added header#app, footer#app etc. to the app.dashboard state. But it would be nice to not repeat this everywhere.
'header#app': {
templateUrl: 'header.html'
},
'app-inner': {
templateUrl: 'dashboard.html'
},
'footer#app': {
templateUrl: 'header.html'
}
Your template index.html has app-outer view in it. app.html has header, app-inner, and footer in it. However, you have header and footer as views of app state, which corresponds to index.html. It looks like it attempts to populate those templates when the parent state first becomes active, before the child state (with app.html) is loaded, so it doesn't find the ui-views for the header and footer states.
In other words, it looks like a ui-view element corresponding to a view must exist at the time the state that defines that view becomes active.
See this Plnkr, which insert another level of wrapping state.

Angular UI Router: inherit only URLs (using parent.child states) without nesting views

I want to inherit states/URLs using parent.child notation in UI router. I don't want to inherit views (or nest views inside other views), or inherit controllers, I just want to inherit URLs. I'm using 'ui.router' as a dependency.
This is an example of a router:
$stateProvider
.state('products', {
url: '/products',
templateUrl: 'view1.html',
controller: 'products#index'
})
.state('products.show', {
url: '/:id',
templateUrl: 'view2.html',
controller: 'products#show'
})
.state('products.buy', {
url: '/:id/:moreParams',
templateUrl: 'view3.html',
controller: 'products#buy'
})
.state('products.sell', {
url: '/:id/:moreParams/:moreParams',
templateUrl: 'view4.html',
controller: 'products#sell'
});
And the controllers are:
angular.module('productsModule', [])
.controller('products#index', function($scope){
})
.controller('products#show', function($scope){
})
.controller('products#buy', function($scope){
})
.controller('products#sell', function($scope){
})
Here, all views are completely different, and I don't want to nest any view inside any other view. Also, all controllers are different too, and I don't want to inherit controllers, they are all separate with different functions.
Here's my expected result. What I want to achieve is Angular to allow me to only inherit URLs, so the URLs become:
/products
/products/:id
/products/:id/:moreParams
/products/:id/:moreParams/:moreParams
(and then have each URL its own view and controller, as specified above)
So far it's not working, and my research is beginning to tell me that this kind of inheritance using parentState.childState is only for when you want to have nested views (which is what I don't want. I only want to re-use URLs).
My workaround is to create router URLs like products_show, that is, using an underscore instead of a dot, so they are treated as new independent URLs rather than inheritance ones. I'm not sure if this is the best idea, mostly because it looks ugly (though it works perfectly).
Perhaps I should just use products_show in case it can't be done with a dot? Ideas?
You can have a parent child relationship between the routes without nesting their respective views. You achieve that by specifying an absolute target for your view. Like this:
$stateProvider
.state('products', {
url: '/products',
templateUrl: 'view1.html',
controller: 'products#index'
})
.state('products.show', {
url: '/:id',
views: {'#': {
templateUrl: 'view2.html',
controller: 'products#show'
}}
})
.state('products.buy', {
url: '/:id/:moreParams',
views: {'#': {
templateUrl: 'view3.html',
controller: 'products#buy'
}}
})
.state('products.sell', {
url: '/:id/:moreParams/:moreParams',
views: {'#': {
templateUrl: 'view4.html',
controller: 'products#sell'
}}
});
By doing that you're basically telling ui-router to render your view inside the unnamed ui-view in your main index.html, thus, overriding the parent view. Instead of looking for a ui-view in the parent view's template.
To understand why this works, you'll have to understand how ui-router decides where to render the view for your route. So, for example, when you do:
.state('parent.child', {
url: '/:id',
templateUrl: 'child-view.html',
controller: 'ChildCtrl'
})
ui-router will by default translate this to something like:
.state('parent.child', {
url: '/:id',
views: {'#parent': {
templateUrl: 'child-view.html',
controller: 'ChildCtrl'
}}
})
which will cause it to look for an unnamed ui-view inside the parent's view template and render the child view's template inside.
You could also specify more than 1 view for a route, like:
.state('parent.child', {
url: '/:id',
views: {
'#parent': {
templateUrl: 'child-view.html',
controller: 'ChildCtrl'
},
'sidebar#parent': {
templateUrl: 'child-view-sidebar.html',
controller: 'ChildViewSidebarCtrl'
}
}
})
In this case, child-view.html will be rendered inside the parent view's unnamed ui-view as before. In addition it will also look for a ui-view="sidebar" in the parent view's template and render child-view-sidebar.html inside.
For more info on this powerful views option and how to specify targets for your view, see the ui-router docs
This is how to write nested states:
$stateProvider
.state('products', {
url: '/products',
templateUrl: 'view1.html',
controller: 'products#index'
})
.state('products.show', {
parent:'products',
url: '/:id',
templateUrl: 'view2.html',
controller: 'products#show'
})

How use two ui-view independents

I have an application where I wish that two panels (ui-view) work singly.
https://jsbin.com/neroze/edit?js,output
But, when I click in link to change the right panel, the center panel is changed and when I click to change the center panel, the right panel is changed.
I wonder how I can work around this problem.
You should have a main state (a parent state) which defines your views (with controllers and templates, etc...)
Then you can have child states where you can override your views using absolute state definitions.
$stateProvider.state('app', {
url: '/',
abstract: true,
parent: 'app',
views: {
'panelLeft#app': {
templateUrl: 'modules/panel/views/leftPanel.html',
controller: 'LeftPanelCtrl as vm'
},
'panelRight#app': {
templateUrl: 'modules/module1/views/anotherView.html',
controller: 'Module1Ctrl as vm'
}
}
}).state('anotherstate', {
url: '/',
parent: 'app',
views: {
'panelRight#anotherstate': {
templateUrl: 'modules/module2/views/differentView.html',
controller: 'Module2Ctrl as vm'
}
}
});
Note: it's just an example from scractch, may not work for the first run.

Angular ui-router not calling child controller in nested routes

I have my routes set up as below. Its too frustrating that the view in view.tab is loaded but its controller isn't called. I tried without the paramaters, but it still doesn't work as expected. Does anyone have any idea on how to solve this?
$stateProvider
.state('index', {
url: '/',
templateUrl: viewsRoot + 'restaurants/index.htm',
controller: 'RestaurantsCtrl'
})
.state('view', {
url: '/view',
controller: 'RestaurantsViewCtrl',
templateUrl: viewsRoot + '/restaurants/view.htm'
})
.state('view.tab', {
url: '/orders',
// controller: 'OrdersIndexCtrl',
controller: function ($scope) {
alert('This does not run');
},
views: {
"view": {
templateUrl: viewsRoot + '/restaurants/orders.htm'
}
}
});
$urlRouterProvider.otherwise("/");
You need to declare the controller along side the template:
views: {
"view": {
templateUrl: viewsRoot + '/restaurants/orders.htm',
controller: 'MyController' // (or a function, etc.)
}
The UI-Router wiki sort of alludes to this:
If you define a views object, your state's templateUrl, template and templateProvider will be ignored. So in the case that you need a parent layout of these views, you can define an abstract state that contains a template, and a child state under the layout state that contains the 'views' object.
Controllers are paired with a view. So if it ignores the "template" properties defined on the state, it seems to imply that it will ignore the controller too.
If you want all of your named views to share a controller, define an abstract parent state as the wiki suggests.

Resources