我应该通过指导和服务之间的数据? [英] Should I pass data between directive and service?

查看:124
本文介绍了我应该通过指导和服务之间的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器具有指令显示与输入的形式。

I have a page with a controller that has a directive that displays a form with inputs.

指令有一个包含所有被显示在表单上输入(和他们的NG-模型)的对象。这种形式的输入数据绑定到该指令里面的对象变量。

The directive has an object that contains all the inputs (and their ng-model) that are displayed on the form. This binds the form inputs data to the object variable inside the directive.

我需要显示的结果(以及其他操作)submiting形式的数据

I need to display results (and other actions) submiting the data of the form.

对于我创建了一个服务处理业务逻辑和thae Ajax调用。

For that I created a service that handles the business logic and thae ajax calls.

这里的问题是... 我应该怎么从执行所需操作的服务访问表单数据?我想过从服务访问指令的变量,但我不知道如何做到这一点,如果这是摆在首位的正确途径。

The questions here is... how should I access the form data from the service to perform the required actions? I thought about accessing the directive variable from the service, but I'm not sure how to do it and if this is the right way in the first place.

推荐答案

服务应持有的模型基本上是表单的JavaScript对象。

The service should hold a model which is basically the javascript object of your form.

指令应注射服务,并增加对他的范围(参照)的对象。

The directive should inject the service and add that object on his scope(a reference).

指令的模板应与指令的范围讲和显示形式。

上的视场改变一个值将反映服务,因为我们它们具有相同的参考和由于存在双向绑定视图将更新指令范围

Changing a value on the view will reflect the service since we they have the same reference and the view will update the directives scope since there is two way binding.

这篇关于我应该通过指导和服务之间的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆