2016-12-21 45 views
2

我试图让多个视图使用相同的控制器。到目前为止,我已经尝试了几件事情,但都没有成功。所谓 “不工作” 我的意思是控制器MapController心不是实例化和意见不能看到控制器UI路由器多个视图共享相同的控制器

$stateProvider.state(PageStateNames.COMPONENTS_LIVEMAP, { 
    url: "/components/vehicles/:vehicle/:panel", 
    views: { 
     "": { 
      controller: "MapController as vm" 
     }, 
     "[email protected]": { 
      templateUrl: "....html" 
     }, 
     "[email protected]": { 
      templateUrl: "....html" 
     } 
    } 
}); 

$stateProvider.state(PageStateNames.COMPONENTS_LIVEMAP, { 
    url: "/components/vehicles/:vehicle/:panel", 
    controller: "MapController as vm" 
    views: { 
     "[email protected]": { 
      templateUrl: "....html" 
     }, 
     "[email protected]": { 
      templateUrl: "....html" 
     } 
    } 
}); 

说完看着existingquestions这应该工作。我错过了什么吗?

回答

0
$stateProvider.state(PageStateNames.COMPONENTS_LIVEMAP, { 
    url: "/components/vehicles/:vehicle/:panel", 
    views: { 
     "": { 
      templateUrl: "......html", 
      controller: "MapController as vm" 
     }, 
     "[email protected]": { 
      templateUrl: "....html", 
      controller: "MapController as vm" 
     }, 
     "[email protected]": { 
      templateUrl: "....html", 
      controller: "MapController as vm" 
     } 
    } 
}); 
+0

这会不会创建控制器3次?理想情况下,我只有一个顶级状态 – Chris

+0

父视图控制器数据可以访问,而无需提及控制器 –

0

要在某个状态下使用相同的控制器,可以使用子父级嵌套状态。例如:

$stateProvider.state('home', { 
    templateUrl: '....html', 
    controller: 'ParentController' 
}) 

.state('home.livemap', { // << this state will use parent controller instance, this is the dot notation to make livemap a child state of home (more info here https://github.com/angular-ui/ui-router/wiki/Nested-States-and-Nested-Views 
templateUrl: '....html' 
});