I have the following AngularJS application composed of a template (index.html
), an app definition (app.js
), a controller definition (controll
When you leave a controller, the scope gets destroyed. I would look into making a service that stores the stuff you want.
angular.module('MyApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {templateUrl:'/index.html', controller: MyController}).
otherwise({redirectTo: '/'});
}])
.service("myService", function(){
this.myAttribute = null;
});
var MyController=['$scope','$http','$location', 'myService',function ($scope, $http, $location, myService) {
//do some fancy stuff
if(myService.myAttribute===null){
myService.myAttribute=someDataFromHttpRequest;
}
$location.search(someParameters);
}];
Services are used to share date between multiple controllers/directives so i'm pretty sure it's what you want.
Here's the doc information on them: http://docs.angularjs.org/guide/dev_guide.services.creating_services