简体   繁体   English

AngularJS在控制器中处理数据的多个部分

[英]AngularJS Processing multiple parts of data in a controller

I'm building an app that basically calculates prices as you pick options. 我正在构建一个基本上可以在您选择期权时计算价格的应用程序。 So basically it's a form with select options and as you go down the form, the app will process the price more refined and change the options for the other form fields. 因此,基本上,这是一个带有选择选项的表格,当您向下浏览表格时,该应用程序将更精细地处理价格并更改其他表格字段的选项。

Currently I have an API built out and in this processing, the app will need to hit the API a few different times. 目前,我已经构建了一个API,在此处理过程中,该应用将需要多次运行API。 There will ultimately be 7-12 form fields that will make up the pricing options. 最终将有7-12个表单字段构成定价选项。 While mapping out the controllers, I'm wondering what the best method to keep my processing organized. 在绘制控制器时,我想知道什么是使我的处理井井有条的最佳方法。 If I was doing this in a language like PHP I'd have different functions per form element and process each time the form changes. 如果我使用PHP之类的语言来执行此操作,则每个表单元素在每次更改表单时都会有不同的功能。

In Angular, having one controller per view, I'm curious how you guys, the pro, would organize it.. 在Angular中,每个视图只有一个控制器,我很好奇专业人士将如何组织它。

In a non-specific language structure I'm thinking something like... 在非特定语言结构中,我在想类似...

  1. select one has 5 options, user selects one. 选择一个有5个选项,用户选择一个。
  2. Using the data from step one, a calculation happens, hits the API and gets new options for select number 2. User selects options and process happens again. 使用第一步中的数据,进行计算,点击API并获得选择编号2的新选项。用户选择选项,然后过程再次发生。

So if my controller is say... 因此,如果我的控制员说...

.controller("formController", function($scope){
        //Function for when select one changes, listen for form change ng-change
        function item1Change(){
           //hit api with item1 value, get options for item2 and and load them into item2
        }
        function item2Change(){
           //hit api with item2 value, get options for item3 and and load them into item3
        }
    })

To me, this seems very dirty as in I need to define stuff, it doesn't seem modular or segmented and I just feel that there's a better option. 对我来说,这似乎很脏,因为我需要定义一些东西,它似乎不是模块化的或分段的,我只是觉得有一个更好的选择。 If you guys have any better ideas, let me know, THANKS! 如果你们有更好的主意,请告诉我,谢谢!

There is no restriction regarding the number of controller available to a view. 对于视图可用的控制器数量没有限制。 A general best practice I have come to follow* is to initialize one controller for each $scope I feel needs to be handled with more care than that available to it's parent scope or the behavior definitons for directives within the scope. 我要遵循的一个一般的最佳实践*是,为每个$scope初始化一个控制器,我觉得需要比其父范围或该范围内指令的行为定义更为谨慎地处理。

I would use: 我会用:

  • a formCtrl 一个formCtrl
  • a priceCtrl priceCtrl
  • a custom directive for the calculated total 用于计算总数的自定义指令
  • a directive strategy for selection behaviors withing the scope paired to the form controller (one or more directives) 一种将行为与表单控制器配对的选择行为的指令策略(一个或多个指令)
  • a service that feeds option configuraitons, dependencies, etc to your controllers and directives as needed (this is a good way to decouple your data from your controller and encapsulate the handling/processing of the data. This also makes it possible for you to provide the same data to different form views if needed 一种根据需要向控制器和指令提供选项配置,依赖项等的服务(这是一种将数据与控制器分离并封装数据处理/处理的好方法。这也使您可以提供如果需要,将相同的数据保存到不同的表单视图

You can think of your directives and directive strategy as being similar to how you would use the decorator pattern. 您可以认为您的指令和指令策略与使用装饰器模式的方式相似。 By decorating the form input with a particular directive, it gains the ability to __ 通过用特定指令修饰表单输入,它可以__

*(this is my own best practice, I can't say it's what the Angular team or community at large endorse) *(这是我自己的最佳做法,我不能说这是Angular团队或社区普遍认可的内容)

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

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