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

Vamshi Vangapally picture Vamshi Vangapally · May 4, 2014 · Viewed 20.9k times · Source

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.

Answer

wilver picture wilver · May 4, 2014

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