0

I just recently started using Angular, and I am trying to redirect the response of http.get to another html page which is result.html. I used services to share data:

Here is my controller code:

  app.controller("controller", function($scope, $http, $window, sharedProperties){
  $scope.generate = function(){
    $http.get('repositories', {params: { username : $scope.username }}).
    then(function mySucces(response) {
      $scope.error = "";
      sharedProperties.setData(response.data);
      $window.location.href = '/result.html';
    }, function myError() {
      $scope.error = "Wrong Username";
    });
  }
});

app.controller("resultController", function($scope, $window,sharedProperties){
  $scope.names = sharedProperties.getData();
  $scope.home = function() {
    $window.location.href = '/index.html';
  }
  });

Here is the services.js code:

app.service('sharedProperties', function() {
    var data;
    return {
        getData: function() {
            return data;
        },
        setData: function(value) {
            data = value;
        },
    }
});

Here is the index.html body:

<div ng-app="App" ng-controller="controller">
    <p>
      <input type="text" ng-model="username"  placeholder="Enter username here" required>
      <button ng-click="generate()">Generate</button>
    </p>

   {{ error }}
</div>

And here is the result.html that I want the response goes to:

<div ng-app="App" ng-controller="resultController">
    <p><button ng-click="home()">Home</button></p>
    <ul>
        <li ng-repeat="x in names">
            {{ "Name: " + x.name + ", Languages: " + x.languages }}
        </li>
    </ul>
</div>

The controller set the response data through sharedProperties so the resultController can use it, however after the page redirects to result.html the result is not shown.

UPDATE

It seems the data is not shared between the two controller, console.log shows undefined. Is it because of 2 different html files? or is it because of redirect?

UPDATE 2

I realized that redirect will reset all the data, so I use sessionStorate for the service:

app.service('sharedProperties', function() {

    return {
        getData: function() {
            return sessionStorage.sharedProperties;
        },
        setData: function(value) {
           sessionStorage.sharedProperties = value;
        },
    }
});

however console.log instead of showing the values of json, it shows:

"[object Object],[object Object],[object Object],[object Object],[object Object]"

The real value is suppose to be:

[{"name":"git-consortium","languages":"No Language Reported"},{"name":"hello-worId","languages":"No Language Reported"},{"name":"Hello-World","languages":"No Language Reported"},{"name":"octocat.github.io","languages":"CSS JavaScript HTML "},{"name":"Spoon-Knife","languages":"CSS HTML "}]

Any thoughts on this?

6
  • 1
    Your angular project implementation totally wrong. Please read and learn about angular.js first. then if any questions appear ask here. :) Mar 28, 2016 at 13:11
  • please make sure your redirection route is defined properly sometime it'll redirect to default case..? Mar 28, 2016 at 13:14
  • (1) You are giving the same name for both the controller.(2) You want to share data between two controllers, so use either services/factories, read it.(3) $window.location.href = 'result.html'; will redirect to result.html and once redirected the below expression $scope.names = response.data; will not get executed.
    – shreyansh
    Mar 28, 2016 at 14:24
  • but even if you want to implement in this way , you can use $rootScope.names=response.data and then redirect to result.html, just a trick , but it may have bad consequences.
    – shreyansh
    Mar 28, 2016 at 14:28
  • thanks for your response @shreyansh
    – secret
    Mar 28, 2016 at 14:31

1 Answer 1

0

Even Though the response that I was getting was json, I had to still convert to json using angular.toJson().

Here is the service to save the json data:

app.service('sharedProperties', function() {
    return {
        getData: function() {
            return angular.fromJson(sessionStorage.sharedProperties);
        },
        setData: function(value) {
           sessionStorage.sharedProperties = angular.toJson(value);
        },
    }
});

So now even after redirecting to another page, I can still access the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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