何时在 AngularJS 应用程序中使用自定义指令、ui-view 和 ng-include? [英] When to use custom directive vs ui-view vs ng-include in an AngularJS application?

查看:19
本文介绍了何时在 AngularJS 应用程序中使用自定义指令、ui-view 和 ng-include?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个大型、复杂的 AngularJS 应用程序(想想 ERP 系统).我很难决定何时适合使用 ui-viewng-includecustom 指令 + templateURL.

I am building a large, complex AngularJS application (think ERP system). I am having a hard time deciding when it is appropriate to use ui-view, ng-include or a custom directive + templateURL.

我将举几个具体的例子,让你们大家都可以使用.

I will give a few concrete examples to give y'all something to work with.

  • 在应用程序的所有 URL 中使用的导航菜单,但包含复杂的 AJAX 自动建议/下拉搜索框.
  • 一个简单的 HTML 页脚,在应用程序的所有 URL 中都相同
  • 位于页眉和页脚之间的内容区域.
  • 位于内容区域内的各个组件(例如编辑个人资料"表单或用户仪表板)
  • 模态对话框

最佳做法是什么?

推荐答案

对于任何大型或复杂的应用程序,我建议将尽可能多的代码组织成可重用的自定义指令.自定义指令将允许您利用角度指令实现最大的可重用性,并最大限度地减少仅依赖内置指令时存在的重复 HTML.

For any large or complex application, I would suggest organizing as much of your code into re-usable custom directives as possible. Custom directives will allow you to leverage angular directives for maximum re-usability and minimize the repetitive HTML that exists when you rely only on built-in directives.

UI 视图适用于根据路由动态交换控制器和视图.如果您有应用程序功能,其中每个视图和控制器组合都是独立的,那么使用带有路由的 ui-view 是有意义的.

UI views are appropriate for swapping out controllers and views dynamically depending on the route. If you have application functionality where each view and controller combination is self-contained, then using a ui-view with routes makes sense.

这篇关于何时在 AngularJS 应用程序中使用自定义指令、ui-view 和 ng-include?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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