0

I am trying to call an API end point once a user clicks a button holding a myNavigator.pushPage() request. However,I can not get the $scope data generated from the $http.get request to be passed to the new page.

If I test using console.log('test'); inside the .success of the $http.get request I successfully get the log info in the console but any data held in $scope.var = 'something'; does not gets passed to the page! Really confused!

$scope.historyDetails = function(id){

    var options = {
      animation: 'slide', 
      onTransitionEnd: function() {
        $http.get('http://xxx-env.us-east-1.elasticbeanstalk.com/apiget/testresult/testId/'+id).success(function(data) {    

          $scope.testscore = 'something'; // this is not getting passed to page!            

          console.log('bahh'); // But I see this in console



        });              
      } 
    };
    myNavigator.pushPage("activity.html", options);        
}

Page:

<ons-page ng-controller="HistoryController">
...

<span style="font-size:1.2em">{{testscore}} </span><span style="font-size:0.5em;color:#555"></span>

...
</ons-page>
condo1234
  • 3,285
  • 6
  • 25
  • 34

2 Answers2

1

Yes, that's so because both pages has different controllers, resulting in different scopes. One can not access variables from one scope to another.

Hence one solution in this case can be using rootScope service. Root Scope is parent scope for all scopes in your angular application. Hence you can access variable of root scopes from any other scope, provided that you are injecting $rootScope service in that controller.

to know more about rootScope check this link.

Good luck.

Update 1:

check these articles

http://www.dotnet-tricks.com/Tutorial/angularjs/UVDE100914-Understanding-AngularJS-$rootScope-and-$scope.html

https://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

Yogesh
  • 1,565
  • 1
  • 19
  • 46
0

As Yogesh said the reason you're not getting your values is because if you look at $scope.testscore and try to find where is the $scope defined you will see that it's an argument for the controller function (thus it's only for that controller).

However we can see that the controller is attached to the page and you are pushing another page.

So in that case you have several options:

  1. Use the $rootScope service as Yogesh suggested (in that case accept his answer).
  2. Create your own service/factory/etc doing something similar to $rootScope.

    (function(){
        var historyData = {};
        myApp.factory('historyData', function() {
            return historyData;
        });
    })();
    

    Technically you could probably make it more meaningful, but maybe these things are better described in some angular guides.

  3. If you have multiple components sharing the same data then maybe you could just define your controller on a level higher - for example the ons-navigator - that way it will include all the pages. That would be ok only if your app is really small though - it's not recommended for large apps.

  4. If this data is required only in activity.html you could just get it in that page's controller. For example:

    myApp.controller('activityController', function($scope, $http) {
        $http.get(...).success(function(data) {
            $scope.data = data;
        });
    }
    

    But I guess you would still need to get some id. Anyway it's probably better if you do the request here, now you just need the id, not the data. You could actually cheat it with the var directive. If you give the activity page <ons-page var="myActivityPage"> then you will be able to access it through the myActivityPage variable.

    And the thing you've been searching for - when you do

    myNavigator.pushPage("activity.html", options);        
    

    actually the options is saved inside the ons-page of activity.html.

    So you can do

    myNavigator.pushPage("activity.html", {data: {id: 33}, animation: 'slide'});
    

    And in the other controller your id will be myActivityPage.options.data.id.

  5. If you still insist on passing all the data instead of an id - here's a simple example. In the newer versions of the 2.0 beta (I think since beta 6 or 7) all methods pushPage, popPage etc return a promise - which resolve to the ons-page, making things easier.

    $scope.historyDetails = function(id){
      myNavigator.pushPage("activity.html", {animation: 'slide'}).then(function(page) {
        $http.get('...' + id).success(function(data) {
          page.options.data = data;
        });
      });
    });
    

Side note: You may want to close the question which you posted 5 days ago, as it's a duplicate of this one (I must've missed it at that time).

Ilia Yatchev
  • 1,254
  • 7
  • 9