角度+ ui-路由器:$stateChangeSuccess由状态b触发,但不是在a.b上。

时间:2022-03-25 10:46:35

ui-router 0.2.11 / AngularJS 1.3.0

ui-路由器0.2.11 / AngularJS 1.3.0。

I struggle to understand why my $stateChangeSuccess event handler in BarCtrl is not triggered on #/foo/bar. It is triggered on #/bar.

我很难理解为什么我的$stateChangeSuccess事件处理程序在BarCtrl中不是由#/foo/bar触发的。它是由#/bar触发的。

#/foo/bar -> Console: foo
#/bar -> Console: bar

What I'm trying to achieve is that on #/foo/bar first FooCtrl's handler is triggered and then BarCtrl's:

我想要实现的是,在#/foo/bar的第一个FooCtrl的处理程序被触发,然后BarCtrl's:

foo
bar

My code:

我的代码:

var app = angular.module('foobar', ['restangular', 'ui.bootstrap', 'ui.router'])
app.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider.state('root', {
    url: "/",
    template: '<div ui-view></div>'
  })
  .state('foo', {
    abstract: true,
    url: "/foo",
    controller: 'FooCtrl',
  })
  .state('foo.bar', {
    url: "/bar",
    controller: 'BarCtrl',
  })
  .state('bar', {
    url: "/bar",
    controller: 'BarCtrl',
  });
})

app.controller('FooCtrl', function($scope) {
  $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
    console.log("foo");
  });
});

app.controller('BarCtrl', function($scope) {
  $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
    console.log('bar')
  });
});

2 个解决方案

#1


3  

While preparing a fiddle I came across my mistakes. I was thinking abstract views would not need a template since they cannot be accessed and assuming the template of foo's parent state would take over, which is not the case.

在准备小提琴的时候,我偶然发现了我的错误。我认为抽象视图不需要模板,因为它们不能被访问,并且假定foo的父状态的模板会接管,但事实并非如此。

To achieve what I wanted I only had to add:

为了达到我想要的,我只需要加上:

  template: '<ui-view />',

Like so:

像这样:

.state('foo', {
  abstract: true,
  url: "/foo",
  controller: 'FooCtrl',
  template: '<ui-view />',
})

A related discussion: https://github.com/angular-ui/ui-router/issues/325

相关讨论:https://github.com/angular-ui/ui-router/issues/325

#2


-2  

You're not specifying the url of #/foo/bar is all.

您没有指定#/foo/bar的url。

You are specifying the state of foo.bar, but the state is only being accessed from the url of #/bar.

您正在指定foo的状态。bar,但是状态只能从#/bar的url访问。

Change the following...

改变以下…

  .state('foo.bar', {
    url: "/bar",
    controller: 'BarCtrl',
  })

... to ...

………

  .state('foo.bar', {
    url: "/foo/bar",
    controller: 'BarCtrl',
  })

... and you should be good.

…你应该很好。

#1


3  

While preparing a fiddle I came across my mistakes. I was thinking abstract views would not need a template since they cannot be accessed and assuming the template of foo's parent state would take over, which is not the case.

在准备小提琴的时候,我偶然发现了我的错误。我认为抽象视图不需要模板,因为它们不能被访问,并且假定foo的父状态的模板会接管,但事实并非如此。

To achieve what I wanted I only had to add:

为了达到我想要的,我只需要加上:

  template: '<ui-view />',

Like so:

像这样:

.state('foo', {
  abstract: true,
  url: "/foo",
  controller: 'FooCtrl',
  template: '<ui-view />',
})

A related discussion: https://github.com/angular-ui/ui-router/issues/325

相关讨论:https://github.com/angular-ui/ui-router/issues/325

#2


-2  

You're not specifying the url of #/foo/bar is all.

您没有指定#/foo/bar的url。

You are specifying the state of foo.bar, but the state is only being accessed from the url of #/bar.

您正在指定foo的状态。bar,但是状态只能从#/bar的url访问。

Change the following...

改变以下…

  .state('foo.bar', {
    url: "/bar",
    controller: 'BarCtrl',
  })

... to ...

………

  .state('foo.bar', {
    url: "/foo/bar",
    controller: 'BarCtrl',
  })

... and you should be good.

…你应该很好。