Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change parent variable using Angular and UI-Router

I have the state brand. On the template of this state, I have some breadcrumbs. When I go to the nested state brand.collections for example, I need to update the breadcrumbs in the brand state.

How can I accomplish this?

.state('brand', {
    url: '/brands/:brandId/:brandName',
    templateUrl: 'js/modules/brands/partials/brand.html',
    controller: 'BrandController',
})

.state('brand.home', {
    url: '/home',
    templateUrl: 'js/modules/brands/partials/brand.home.html',
    controller: 'BrandController'
})

.state('brand.collections', {
    url: '/collections',
    template: 'Some collections',
    controller: 'CollectionsController'
})
like image 914
Patrick Reck Avatar asked Feb 10 '26 22:02

Patrick Reck


1 Answers

inside CollectionsController

$scope.$emit('changeParentVar',<newVal>);

inside BrandController

$scope.$on('changeParentVar',function(event,newVal){
event.stopPropagation();
variable = newVal;
});

Reference http://docs.angularjs.org/api/ng.$rootScope.Scope#methods_$on

like image 196
himangshuj Avatar answered Feb 13 '26 15:02

himangshuj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!