Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to update a global variable within an $http request, redirect to a new page and then print that variable (on error). I'm not sure if i'm doing this correctly, but when I try to print my variable ($rootscope.adHocLevel2) it remains empty. In js file

$scope.searchStep2 = function(option) { 
  $http.post($scope.url, { "option" : option}).
    success(function(data, status) {
      $scope.status = status;
      $scope.data = data;
      $rootScope.response = data; 
      })
    .
    error(function(data, status) {
      $scope.data = data || "Request failed";
      $scope.status = status;   
      $rootScope.response = 'Request failed';
      $rootScope.routeAd = {"route": "../adHoc/adhoc.html"};            
      $rootScope.addHocLevel2 = {"suggestedCategory": "Car","suggestedLocLabel": "world","suggestedShadowURL": "http://www.toyota.com/auris/hybrid"};
      $window.location=$rootScope.routeAd.route;            
});
};

In Html I'm trying to print

<input type="text" ng-model="optLocation" value={{addHocLevel2}} />

Thanks ahead

share|improve this question
    
Could you provide a plnkr example? –  user2273266 Jun 3 '13 at 8:37
    
@user2273266 plnkr.co/edit/kDSAuqroSJ0Zd94JHtuY?p=preview –  Gidon Jun 3 '13 at 9:10

1 Answer 1

up vote 2 down vote accepted

Based on the above code, you have $window.location = $rootScope.routeAd.route which points to "../adHoc/adhoc.html". This will reload the app and therefore you lose addHocLevel2 value.

You will need to redirect to its hash route path (e.g. #/adhoc) - as per your routeProvider.when("/adhoc") config, rather than ../adHoc/adhoc.html.

Also I think the best way when moving within the angular's one page app is to use $location.path(yourRoutePath) rather than $window.location unless you want to change location entirely (new URL somewhere).

e.g

You have 2 routes setup:

app.config(function($routeProvider){
    $routeProvider
        .when('/main', { templateUrl:'/main/main.html',controller:'MainCtrl'})
        .when('/adhoc', { templateUrl: '/adHoc/adhoc.html',controller:'AdHocCtrl});
}

Then say you want to redirect from /main to /adhoc, it goes something like this:

app.controller('MainCtrl',function($rootScope,$scope,$location,$http){
    $http.post(...).error(function(data,status){
       $rootScope.addHocLevel2 = {...};
       $location.path("/adhoc"); // this will 'redirect' you to adHoc/adhoc.html page
    }
})

Update: I updated your plunker: http://embed.plnkr.co/01lfzd4Q4bFoSPAB4WIF/ This is how I would do it - I'm sure there are another approaches. I use index.html as the template and have main.html and adhoc.html as the 2 other pages that you can switch to based on the URL.

share|improve this answer
    
thanks for the answer. But, I changed the route to $rootScope.routeAd = {"route": "../adHoc/#adhoc.html"}; and I'm getting an error, is the syntax correct? –  Gidon Jun 3 '13 at 9:03
    
Could you please provide your routerProvider settings? I updated the above with more explanation. try $location.path("adhoc") or $location.path("adhoc.html") depending on the 'routerProvider.when()' set in the routerProvider. –  maethorr Jun 3 '13 at 9:09
    
Thanks, the answer was very helpful, I worked it out :) –  Gidon Jun 5 '13 at 5:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.