0

这个应用程序有许多下拉菜单和单选集,它们的选项是完全静态的,例如,可以在 HTML 中硬编码。这样做是否被认为是 Angular 的最佳实践,或者在适当的控制器中定义描述它们的数据,然后渲染出来?

对浏览器来说,硬编码显然减少了工作量,但可能并不显着。渲染数据在 HTML 中的代码更少,但在控制器中更多。

你会单元测试这些选项是否符合预期?如果您将单元测试视为要构建的规范,而不仅仅是对预期功能的验证,那么您可能会这样做。据我所知,您希望将它们定义为模型数据来做到这一点。

这方面的典型做法是什么?

4

1 回答 1

1

If you can hard code it, go ahead. It's not going to break anything. You wouldn't have to test a hard-coded select, you could just trust that it was working. Likewise, if you just had an array that was only used to create the select, you wouldn't really have to test that either; because you could just trust that JavaScript works the way it's intended.

I guess you could just weigh the pros and cons:

  • Hard-coded
    • Pros
      • faster to render.
      • less overhead.
      • easy to understand
    • Cons
      • more typing.
      • data not as reusable.
  • From an Array
    • Pros
      • less typing.
      • slightly more reusable by other code.
      • easier to add new items (if you're lazy, haha)
    • Cons
      • more overhead
      • select takes longer to render out.

"Best practice" is whatever is easiest to maintain, testable, and works.

于 2012-12-06T13:51:10.603 回答