Browse through screens taking information

0

I'm working with AngularJs and Ionic, creating a mobile application. I would like to know how best to navigate between screens taking information from one screen to another. For example: On a signup screen I want to go to the product screen and come back with the selected product, then go to seller screen and return again to signup screen now with seller and product. At the moment I send the information through the URL, example: incluir/1/1 - each number represents a code that I can fetch from the bank later. Another way is to use windows.localstorage['vendedor'] that stores in the device memory. Is there a more practical way? Share it, thank you.

    
asked by anonymous 28.10.2015 / 11:47

4 answers

2

There is also another way of sending information from one screen to another, which is by saving an object in the application scope ($ rootScope).

Example:

$rootScope.venda = {
   cliente: 1,
   produto: 1
};
    
30.10.2015 / 00:09
3

We do not recommend using $rootScope to do what you are looking for. The services ( service and factory ) of AngularJs are there for this.

Why not use $ rootScope?

$rootScope , as the name already says, is a root scopo, common and accessible in all controller that you own. If you create a $rootScope with the name sales: $rootScope.vendas it will be accessible in all controller , can be changed, deleted and / or inserted a new value.

Working with short and simple applications, no problem. But when you start working with multiple controller who need this data, you can eventually create a $scope that has nothing to do with $rootScope but modifies that value. Or you end up losing control of which function is modifying that $rootScope .

Another point, if the user leaves the application and comes back later, will he be able to continue from where he left off? Using the same data stored previously? Or will $rootScope be reset to its initial state?

Note: Most importantly, do not be tempted to create a function using $rootScope .

When to use $ rootScope?

We recommend using $rootScope only for global settings that do not change. Ex .: Name of the application, name of the user logged in welcome message (Still to analyze this), anyway. Data that you will not modify (or at least not so often).

What to use?

Since you already said that you are using localStorage , I recommend that you stick with it. Your control will be much better about the data you need / manipulate, as well as being able to reuse the data if the user exits the application and comes back later. You can still set an expiration time for that information.

Note: It still keeps url clean, without a lot of data.

Observations

Of course all this should be taken into consideration by analyzing the size / complexity of your application vs. actual improvements / optimizations that you will have.

The use of $rootScope should really be avoided, but it is not always feasible to create this whole scenario to manipulate the data if it is something simple.

    
25.11.2015 / 14:36
1

The most common practice is to use query parameters, in this case exactly how you are using them. So by loading a driver, it's simpler to know if the route in question is for editing or re-registering.

    
28.10.2015 / 17:24
1

I do not recommend using Jquery with Ionic: the philosophy of jquery is to manipulate the DOM directly, which goes against the philosophy of angularjs that manipulates the DOM through directives. There is also a performance issue.

You can save objects to $ rootScope, however, it is better to model your data so that it is provided by factory .

Factories are singletons, that is, defined and initialized only once.

For example, you can set a factory UserSession where you store all the variables related to the user in that session: product-chain, shopping cart, etc. and access it from any controller in the app.

    
25.11.2015 / 14:07