Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

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

share|improve this question
1  
You can create a custom service and share the data between the controllers – Alon Eitan Nov 25 '15 at 16:19
    
I completely fail to understand why you would need a shared variable between two unrelated controllers in order to prevent a modal to appear when a link is clicked. Those seem two completely orthogonal problems to me. – JB Nizet Nov 25 '15 at 16:19
    
If you have a parent/child controller relationship then the child can access the parent's scope via $scope.$parent.xyz. For example: stackoverflow.com/questions/21453697/…. – jarmod Nov 25 '15 at 16:24

I would suggest you can use $watch on your food object/var if any changes happen $broadcast it your change to all and then in your child/modal control catch it with $on.

Hope this help!

share|improve this answer

Take a look at Angular Services.

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.

Also:

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.