指定AngularJS控制器:使用ngController主场迎战$ routeProvider好处 [英] specifying AngularJS controller: benefits using ngController vs. $routeProvider

查看:159
本文介绍了指定AngularJS控制器:使用ngController主场迎战$ routeProvider好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法(据我所知)到一个控制器和视图模板/部分相关联:中 $ 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 ,另一方面提供了先进的功能,如通过决心注射值的财产路线。这样,你可以有你的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.

顺便说一句:如果你需要路由和嵌套的意见你可以看看角UI路由器

Btw: If you need routing and nested views you can take a look at angular ui-router

这篇关于指定AngularJS控制器:使用ngController主场迎战$ routeProvider好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆