Is there another way other than creating a factory or service todo this?
Asked
Active
Viewed 456 times
0
-
After asking this I realized how much I shouldn't do this. **Use a factory**!!! – fauverism Nov 25 '14 at 20:24
-
1http://stackoverflow.com/questions/21904174/two-views-in-one-angularui-router-state-sharing-scope/21924873#21924873 – cheekybastard Nov 26 '14 at 01:22
3 Answers
3
There are a few ways to share data between state controllers:
- Put the data on a common ancestor's
$scope
, so all the controllers in question see the data on their local scopes. - Use a global service (as you said).
- Create a "local service" (using the
resolve
config) in a common ancestor's state. - Pass the data as
$stateParams
(useful only in some very specific situations).
The first one is usually the best bet, as putting the data on $scope
also allows you to easily $watch
the changes.

hon2a
- 7,006
- 5
- 41
- 55
0
Well this may not be the best but it works for me pretty well in an app i develop, and the trick was using the local storage, for example in my search view:
controller: 'SearchCtrl'
state: 'search'
i get a list of results from a web service and stored them like this:
localStorage.setItem('results', angular.toJson(results)); // assuming a json response
then i made a common transition to another state $state.transitionTo('results')
:
controller: 'ResultCtrl'
state: 'results'
and finally get the results from the localStarage
in a scope variable of ResultCtrl
:
$scope.results = angular.fromJson(localStorage.getItem('results'));

Strife86
- 1,135
- 1
- 10
- 18
0
You can have your routes like this:
.state('parent', {
url: "/parent",
abstract: true,
controller: 'ParentCtrl'
})
.state('app.foo', {
url: "/foo",
views: {
'menuContent' :{
templateUrl: "templates/foo.html",
controller: 'FooCtrl'
}
}
})
And access from FooCtrl
the functions and data that are in ParentCtrl

Mati Tucci
- 2,826
- 5
- 28
- 40