问题描述
有两种方法 (AFAIK) 将控制器与视图模板/部分相关联:$routeProvider
中指定的路由和 ngController
指令.特别是(但不仅限于)对于简单路由,两者之间是否有任何优势/效率?
There are two ways (AFAIK) to associate a controller with a view template/partial: the route specified in $routeProvider
and the ngController
directive. Especially (but not exclusively) for simple routing, is there any benefit/efficiency of one over the other?
我的项目目前使用 $routeProvider 方法,但我被赋予了嵌套视图的任务.这对于 ngInclude 来说似乎很简单,只要部分指定了它的 ngController.
My project currently uses the $routeProvider approach, but I've been given the task of nesting views. This seems simple enough with ngInclude, as long as the partial specifies its ngController.
推荐答案
如果你认为一个包含所有脚本的视图是一个独立的包,由一个人或团队开发,那么 ngController
是要走的路,恕我直言.
If you think of a view including all scripts as a self-contained package, developed by a single person or team, then ngController
is the way to go, imho.
$routeProvider
另一方面为您提供高级功能,例如通过路由的 resolve
属性注入值.这样你就可以让你的 AJAX 加载的数据直接注入你的控制器,例如,而不是控制器让它自己获取.或者更改路线以等待该数据等
$routeProvider
on the other hand provides you with advanced features like injection of values via the resolve
property of the route. That way you can have your AJAX loaded data directly injected into your controller, e.g., instead of the controller having it to get itself. Or have the route change to wait for that data etc.
顺便说一句:如果你需要路由和嵌套视图,你可以看看angular ui-router
Btw: If you need routing and nested views you can take a look at angular ui-router
这篇关于指定 AngularJS 控制器:使用 ngController 与 $routeProvider 的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!