2015-09-18 99 views
1

我试图创建应默认去的ChildState父状态。 我需要能够有一个parentController和ChildControllers。 我尝试以下,但你可以看到我的childControllers不叫。UI路由器父状态,孩子指出控制器

var app = angular.module('test', ['ui.router']); 

app.config(config); 

app.controller('ParentCtrl', ['$scope', function ($scope) { 
    console.log('parentCtrl'); 
}]); 

app.controller('Child1Ctrl', ['$scope', function ($scope) { 
    console.log('Child1Ctrl'); 
}]); 

app.controller('Child2Ctrl', ['$scope', function ($scope) { 
    console.log('Child2Ctrl'); 
}]); 

状态:

function config($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/parent'); 

    $stateProvider.state('parent', { 
     abstract: true, 
     views: { 
      'main': { 
       controller: 'ParentCtrl', 
       controllerAs: 'parentCtrl', 
       templateUrl: 'page.html' 
      } 
     }, 
     url: '/parent/' 
    }); 
    $stateProvider.state('parent.child1', { 
     views: { 
      'main': { 
       controller: 'Child1Ctrl', 
       controllerAs: 'child1Ctrl', 
       templateUrl: 'page.html' 
      } 
     }, 
     url: 'child1?param1', 
    }); 
    $stateProvider.state('parent.child2', { 
     views: { 
      'main': { 
       controller: 'Child2Ctrl', 
       controllerAs: 'child2Ctrl', 
       templateUrl: 'page.html' 
      } 
     }, 
     url: 'child2?param2', //is this /parent/child2... ? 
    }); 
} 

你也可以在这里找到代码:https://jsfiddle.net/vjgh8ntL/2/

有人能指导我在正确的方向?

回答

1

我想在这种情况下只使用其它方式不同:

$urlRouterProvider.otherwise('/parent/child1?child1=null'); 

入住这working example

有类似Q &一个(甚至不同的解决方案,例如.when()设置)

+0

感谢您的例子,它证明了我所需要的一切 – yeouuu

+0

那真的是很高兴地看到,先生;)享受强大的UI,路由器... –

相关问题