Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my $scope attributes keep resetting in my AngularJS application?

I have the following AngularJS application composed of a template (index.html), an app definition (app.js), a controller definition (controllers.js) and a hosting page (host.jsp).

The code is as follows:

search.jsp

<div class="container-fluid" ng-app="facetedSearch">
    <div class="row-fluid" ng-view>
    </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src="/resources/js/page/search/app.js"></script>
<script src="/resources/js/page/search/controllers.js"></script>

app.js

angular.module('MyApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
     when('/', {templateUrl:'/index.html', controller: MyController}).
     otherwise({redirectTo: '/'});
}]);

controller.js

var MyController=['$scope','$http','$location',function ($scope, $http, $location) {
   //do some fancy stuff
   if($scope.myAttribute===undefined){
      $scope.myAttribute=someDataFromHttpRequest;
   }
   $location.search(someParameters);
}];

index.html and host.jsp are not shown for brevity and their irrelevance.

The controller gets some data from Ajax request, stores some of it in the $scope to avoid requesting it again and the shows it to the user and waits for input. When the user selects some data in the view, I update URL query part to reflect selection changes. But I want to avoid subsequent Ajax requests by checking if the data is available in the $scope.

The problem I'm facing is that the $scope.myAttribute is always undefined. It resets on every request. I think I'm misusing AngularJS. Any clue?

like image 566
Daniel Cerecedo Avatar asked Dec 18 '12 18:12

Daniel Cerecedo


People also ask

Can $scope be injected while creating service?

Explanation: No, the '$scope' cannot be injected while creating service using 'factory' method.

What is the responsibility of the controller in AngularJS?

The primary responsibility of the controller is to create a scope object and thereby pass it to the view. With this, we come to an end of this AngularJS Controllers article.

What is AngularJS life cycle?

Now that you understand the components involved in an AngularJS application, you need to understand what happens during the life cycle, which has three phases: bootstrap, compilation, and runtime.

What is scope and controller in AngularJS?

The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.


1 Answers

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

like image 130
Mathew Berg Avatar answered Oct 13 '22 20:10

Mathew Berg