你必须在视图中使用 ng-Controller 还是你可以在 $routeProvider 中声明它? [英] Do you have to us ng-Controller in the view or can you just declare it in the $routeProvider?

查看:18
本文介绍了你必须在视图中使用 ng-Controller 还是你可以在 $routeProvider 中声明它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近意识到我没有使用 ng-Controller 在 HTML 中声明我的控制器的官方范围,但只在 $routeProvider 和所有 APPEARS 中提到它是好的.

I recently realized I had not used ng-Controller to declare the official scope of my controller in the HTML but had only mentioned it in $routeProvider and all APPEARS to be well.

有没有人知道离开它有什么坏处?仅在 $routeProvider 中定义和引用我的控制器?

Does any one know any disadvantage to leaving it as such? Only defining and the making reference to my controller in the $routeProvider?

推荐答案

我认为它是正确的,并且你这样做的方式是一个优势,因为它通过将事物保留在视图和绑定控制器的上下文中更好地关联一处.

I would consider it correct, and an advantage the way you have done it, because it better relates the context of your view and bound controller by keeping things in the one place.

请记住,在路由定义中提供控制器与将 ng-controller="MyCtrl" 放在包含 ng-view 与您的路线视图相匹配的指令.

Just bear in mind that providing your controller in the route definition is basically the same as putting an ng-controller="MyCtrl" inside the element containing your ng-view directive that matches your route view.

此外,如果您需要更复杂的路由(例如:分层视图和控制器),那么我会考虑查看 ui-router 代替 ngRoute

Also, if you need more complex routing (eg: hierarchical views and controllers), then I would consider looking at ui-router to use instead of ngRoute

这篇关于你必须在视图中使用 ng-Controller 还是你可以在 $routeProvider 中声明它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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