[英]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... 在非特定语言结构中,我在想类似...
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: 我会用:
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.