简体   繁体   English

正确的方式以角度构造页面

[英]Proper way to structure a page in angular

I had an interesting discussion with one of my colleagues. 我和一位同事进行了有趣的讨论。 We are building a complex UI using angular, we have 2 different approaches 我们正在使用angular建立一个复杂的UI,我们有2种方法

  1. Use 1 controller for the whole page, it's simple, everything will just work 在整个页面上使用1个控制器,很简单,一切都可以正常使用
  2. Break the page into multiple smaller directives, each inherits the main controller's scope. 将页面分成多个较小的指令,每个指令都继承主控制器的作用域。 The main controller fetches the data and passes it down to child directives (well not really pass it down, more like the children can just get it from the scope). 主控制器获取数据并将其传递给子指令(很可能并没有真正传递给孩子,更像孩子可以从范围中获取它)。 Basically, each child directive handles one portion of the UI. 基本上,每个子指令处理UI的一部分。 This approach requires more files and is more complex 此方法需要更多文件,并且更复杂

I am looking for more options and/or opinions about the 2 approaches mentioned above. 我正在寻找有关上述2种方法的更多选择和/或意见。

Checkout the Angular Style guide by John Papa https://github.com/johnpapa/angular-styleguide 查看John Papa撰写的Angular Style指南https://github.com/johnpapa/angular-styleguide

This style guide contains all the best practices you need to know about AngularJS 该样式指南包含您需要了解的有关AngularJS的所有最佳实践

Since you said you build a complex UI, I would strongly recommend using the second appraoch. 由于您说过要构建复杂的UI,因此我强烈建议您使用第二种方法。 The first one has just one average: its faster to write. 第一个只有一个平均值:它的编写速度更快。 The disadventages outweigh this by far. 灾荒远远超过了这一点。 It will grow fast and you will lose track of what is happening what might lead to unsuspected behaviors. 它会快速增长,您将无法掌握正在发生的情况,而这可能导致无法预料的行为。 Its much harder to test and maintain. 它很难测试和维护。 The second approach is also better when it comes to reusability which improves code quality. 当涉及可重用性时,第二种方法也更好,它提高了代码质量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM