简体   繁体   English

angularjs数据组织服务与范围

[英]angularjs data organisation service vs Scope

Data case: 100 different meals , with all there own origin (country), preparation time, difficulty etcetera). 数据示例: 100种不同的餐食 ,所有餐食都有自己的来历(国家/地区),准备时间,难度等)。

the two active views (next to each other); 两个活动视图 (彼此相邻);

  1. filter (by country, preparation time, difficulty ...) 筛选器(按国家/地区,准备时间,难度...)
  2. List (meals which meet the filter criteria) 清单(符合过滤条件的餐点)

-What are the advantages of putting all the meals in a meal Service with respect to putting them directly on the scope? -什么是把所有的饭菜一餐服务相对于把他们直接在范围的优势是什么?

filtered list 筛选清单

  • should I have an object inside the mealservice which holds the filters (eg filters={'country':'germamy', 'maxPrepTime':50} 我应该在mealservice内部有一个包含过滤器的对象(例如, filters={'country':'germamy', 'maxPrepTime':50}

  • should I have an object inside the mealservice which holds the selected meals, if for example an country is added to the searchcriterea this list is directly updated (in the view only ng-repeat will be required for this list) 我应该有内部的对象mealservice保持所选择的膳食,例如,如果一个国家被添加到searchcriterea该列表被直接更新(在视图中仅ng-repeat将需要此列表)

  • should I define my own filter ( ng-repeat="meal in meals" | customFilter " ) 我应该定义自己的过滤器( ng-repeat="meal in meals" | customFilter "

  • other method? 其他方法?

Thanks in advance!!! 提前致谢!!!

As a best practice you should always have fat models and thin controllers . 作为最佳实践,您应该始终具有胖模型和瘦控制器

One of the most important arguments to make so is, that it will be much easier to reuse your code if it's encapsulated in a service/factory then if it's implemented in the controller. 这样做的最重要的论据之一是,如果将代码封装在服务/工厂中,然后再在控制器中实现,则重用代码会容易得多。

You can also implement you own filter, but it can actually use a method from the service. 您也可以实现自己的过滤器,但实际上可以使用服务中的方法。

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

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