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

查看:34
本文介绍了如何在 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.

开发

推荐答案

看看 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- 控制器之间

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

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