[英]AngularJS: Best practices for routes and controllers
在我们公司上,我们目前正在开发单页Web应用程序。 用户可以登录并使用数据填充其个人资料,并与其他订户共享(这是一种类似于社交网络的应用程序)。 现在,我已经想到了以下模式:
我的问题是:将路由(例如“ view-profile”)与特定控制器方法(ProfileController.viewProfile)关联的最佳方法是什么? 在AngularJS中这是正确的模式吗?
谢谢!
进一步调查发现,获得所需结果的最佳方法是使用状态配置中的resolve,如本文中所述: https : //scotch.io/tutorials/making-skinny-angularjs-controllers 。
编辑:我会尽力解释一下。 我要实现的目标是将特定的应用程序状态路由到控制器方法 ,以避免每次我访问控制器时都预先加载大量数据。 假设我的ui路由器有这种情况
.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。 但幸运的是,我们可以在ui-router配置上使用resolve
属性,以避免为特定路由预加载无用的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.