使用Angular和UI-Router更改父变量

时间:2014-02-06 12:05:19

标签: angularjs angular-ui-router

我有州brand。在这个州的模板上,我有一些面包屑。例如,当我转到嵌套状态 brand.collections时,我需要更新brand状态的面包屑。

我该如何做到这一点?

.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'
})

1 个答案:

答案 0 :(得分:3)

内部的CollectionsController

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

在BrandController里面

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

参考http://docs.angularjs.org/api/ng。$ rootScope.Scope#methods_ $ on