进一步调查我发现实现我想要的结果的最佳方法是在状态配置中使用解析,如本文所述:https ://scotch.io/tutorials/making-skinny-angularjs-controllers 。
编辑:我会试着解释一下。我想要实现的是将特定的应用程序状态路由到控制器方法,以避免每次访问控制器时预加载大量数据。假设我的 ui-router 有这种情况
.state('view-profile', {
url: '/view-profile',
templateUrl: 'components/profile/profile.html',
controller: 'ProfileController'
})
[...]
.state('edit-profile', {
url: '/edit-profile',
templateUrl: 'components/profile/edit-profile.html',
controller: 'ProfileController'
})
由于 ProfileController 可能会做很多与编辑/查看无关的事情,因此我想设置一个特定的控制器方法,以便在导航到这些 url 时调用。在其他 MVC 框架中,您可以将路由关联到特定的控制器方法,因此您将拥有
route: /edit-profile -> resolvesAs: ProfileController.editProfile(args)
不幸的是,在 AngularJS 1.4 中并没有真正的方法来做到这一点,至少不使用 ui-router。但幸运的是,我们可以使用resolve
ui-router 配置上的属性来避免为特定路由预加载无用数据。