如何在AngularJS的多个模块之间共享变量 [英] How to share a variable between multiple modules in AngularJS

查看:120
本文介绍了如何在AngularJS的多个模块之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AngularJS的新手,了解基本原理.但是,我现在遇到的情况是我还不知道如何解决该问题.

I'm new to AngularJS and understand the basic principles. However, I'm having a case now where I don't yet know how to get to the solution.

让我简要说明一下情况.假设您有一家超市网上商店,那里有一个输入框,您可以在其中输入内容以搜索食物.在输入框旁边,有一个图标,显示单击时的模式以及所有订购最多的食物.

Let me briefly explain you the case. Let's say you have a supermarket webshop where there is an input box where you can type to search for your food. Next to the input box, there is an icon that displays a modal on click with all the most ordered foods.

此模式具有其自己的控制器,而输入框所在的搜索面板具有其自己的控制器.

This modal has it's own controller and the searchpanel in which the inputbox is has it's own controller.

现在,当食物售罄时,它就会在模式中被删除.但是,当所有受欢迎的食物都卖完了时,我希望模式不会在点击时显示.

Now when a food is sold out this gets striked through in the modal. But when every popular food is sold out I want the modal not to show on click.

我以为可以为模态的范围分配一个变量,但是后来我无法在搜索栏的范围内访问它.我认为我可以使用rootScope,但是我真的很想知道是否有更好的解决方案.

I thought I could assign a variable to the scope of the modal but then I couldn't access it in the scope of the searchbar. I could work with rootScope I think, but I would really like to know if there is a better solution.

在此先感谢您,如果您需要更多信息,请告诉我.

Thanks in advance and if you need more info, please let me know.

adev

推荐答案

看看 Angular Services .

Angular服务是可替换对象,这些对象使用依赖项注入(DI)连接在一起.您可以使用服务来组织和共享应用程序中的代码.

Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.

也:

passing-data-between-controllers-in-angular-js

how-can-i-pass-variables -between-controllers

这篇关于如何在AngularJS的多个模块之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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