S
S
Stepan Kormilin2015-12-01 14:43:26
Angular
Stepan Kormilin, 2015-12-01 14:43:26

What is the correct way to create a global object in an Angular application?

Good afternoon!
In application I get an array of type $scope.myObject = [];
Adding objects to this array occurs, let's say, from one page, deleting - from another. How to make it so that at any time from any page this array is up-to-date? Each time I add and remove data, I send it to the server, that is, each time I get a new instance of the array from there. But apparently I got confused in scopes, because on different pages I have a different array in the console. Tell me how to implement correctly? In fact, it's like adding/removing items to the shopping cart in an online store.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2015-12-01
@Div100

1. Make a separate service, the field is an array, methods for managing this field, like getters and setters for it. You can use data change events, for example. $rootScope.emit('dataChanged')
2. Use global scope. $rootScope.myObject = [];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question