我想知道构建 angular.js 应用程序的最佳方法。我相信所有指令都通过 ajax 请求下载各自的模板。因此,最好将 SPA 的每个不同部分作为单独的单元包含在指令中,并具有自己的模板 html 和 Controller :
<html>
<head>
</head>
<body>
<section>
<directive1></directive1>
</section>
<section>
<directive2></directive2>
</section>
<section>
<directive3></directive3>
</section>
</body>
</html>
或者最好保留 DOM 操作指令并将所有 Controller 嵌入到主 html 页面中:
<html>
<head>
</head>
<body ng-app="myapp">
<section ng-controller="controller1">
...
</section>
<section ng-controller="controller2">
...
</section>
<section ng-controller="controller3">
...
</section>
</body>
</html>
我想知道每种方法在性能、最佳实践等方面的优点和缺点是什么。 提前致谢。
最佳答案
对于任何未开发的项目,我全心全意地建议您应尽一切努力避免在应用程序中使用 ng-controller
。
如果/当您尝试将任何应用程序升级到 AngularJS 2.0 版本(无论何时发布),这将节省很多心痛。它与当前版本有很大不同,其中之一就是不再有 ng-controller
。
我们发现,它们鼓励开发人员走捷径,而不是设计模块化的松散耦合的组件/指令,我们最终得到的是紧密耦合的 n 层 Controller 层次结构。
我发现这是一篇非常好的文章,考虑到 Angular 2.0 所声明的方向,其中的主题值得考虑
http://teropa.info/blog/2014/10/24/how-ive-improved-my-angular-apps-by-banning-ng-controller.html
在项目开始时设计自动使用 ng-controller
的指令可能会很困难,但我认为从长远来看这是最好的方法。
考虑到过去几年中开展的大量 Angular 项目,组织内部将会进行许多对话,以评估他们是否能够投入所需的精力来摆脱 1.2-1.3 并从本质上重写应用程序以便能够使用 2.0。
关于javascript - 如何构建 angular.js 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29457901/