简体   繁体   中英

Why is angular-promise-tracker not working for me?

I've installed angular-promise-tracker and I think I'm close to getting it working. The problem I'm having is that the "loading" text isn't showing. The data is fetched and does show when outputting it to the console.

So it appears that ng-show="loadingTracker.active()" isn't working. I can't see what I'm doing wrong with this.

Any help with this would be sincerely appreciated - as always :)

Here's my code:

HTML

<button ng-controller="weatherCtrl" 
        ng-model="hotel" 
        class="btn btn-default" 
        ng-click="loadWeather(hotel, $index)">
        {{buttonText}} more</button>
<div collapse="isCollapsed">
    <div class="well well-lg more-detail">
        {{hotel.Description}}
        <br /><br />
            <div>
                <div class="my-super-awesome-loading-box" 
                     ng-show="loadingTracker.active()">
                     Loading..
                </div>                        
            </div>
    </div> 
</div>

JS

.controller('weatherCtrl', function ($scope, weather, $timeout, promiseTracker){
   $scope.loadingTracker = promiseTracker();
   $scope.buttonText= 'Load'
   $scope.loadedHotelDetails=[];
   $scope.loadWeather = function(hotel, index) {
      // console.log('loadWeather')           
      weather.get({tracker: $scope.loadingTracker}, function (data){
      console.log(data)
      $scope.loadedHotelDetails.push(index)
   })
})



angular.module('weather', [])
    .factory('weather', function($http) {
         var weather = {};
         weather.get = function(params, callback) {
            $http.get('/php/weather.php', {params: {tracker: params.tracker, page: params.page}}).success(function(data) {
             callback(data);
            });
         };
         return weather;
     });

I've never used this module, but from the examples on github, I think you're supposed to call it like this in weather :

     weather.get = function(params, callback) {
        $http.get('/php/weather.php', {tracker: params.tracker, page: params.page}).success(function(data) {
         callback(data);
        });
     };

Got it sorted, this is the code I needed:

.factory('weather', function($http) {
  var weather = {};
  weather.get = function(params) {
    return $http.get('/php/weather.php', {tracker: params.tracker, page: params.page});
  };
  return weather;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM