Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $state.go(app.location) and $location.path("app/location")?

I have recently started using ionic framework, it has angular js in it. To navigate between screens, I was using $location.path and it's working great. However, in an example I've downloaded, I saw $state.go being used to redirect to some page. I would like to know the difference between the two.

like image 826
Vamshi Vangapally Avatar asked May 04 '14 09:05

Vamshi Vangapally


People also ask

What is html5mode?

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents.

What is the purpose of $location service in AngularJS?

Overview. The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.

How to change URL path in AngularJS?

To change path URL with AngularJS, $location. path() is passed the new URL as a parameter, and the page is automatically reloaded. This means that if you ever make a change to the URL in an Angular application, then the partial and controller will be reloaded automatically.


1 Answers

The $location service is on the angular.js framework out of the box and allow you to manage location object (similar to that in pure javascript). The $state service is a part of ui-router module and allows you to manage routes in an advanced mode, throughout a state machine management of views.

If you use ui-router, you should prefer to use $state service to manage states/routes because state abstracts the concept of route and you could change the physical routes without changing states.

Besides that, more problems you could have if you run in hashbang mode, in particular in your html links. In this case it's preferable to use ui-sref rather than ng-href (or just href). In my opinion, you should always think in term of states rather than paths. Obviously you can mix the services if you know what you're doing

like image 59
wilver Avatar answered Oct 14 '22 10:10

wilver