1

I want to send $stateparams.id along with the below,

HTML:

<ion-refresher pulling-text="Pull to refresh..." on-refresh="vm.loadList(true)">
</ion-refresher>
<ion-list>
   <ion-item class="item-remove-animate item-thumbnail-left item-icon-right" ng-repeat="data in vm.menuItems" href="#/app/menu-detail/{{data._id}}" type="item-text-wrap">
      <img ng-src="app/data/images/{{data.imageUrl}}">{{data.categoryName}} <br />
      <p ng-show="{{data.is_recommended}}"><span class="gem-label warning">Chef Special</span></p>
      <i class="icon ion-chevron-right icon-accessory"></i>
   </ion-item>
</ion-list>

Controller:

vm.loadList = function (forceRefresh) {
                appealityApi.getMixedMenu(forceRefresh,$stateParams).then(function (data) {
                    vm.menuItems = data
                }).finally(function () {
                    $scope.$broadcast('scroll.refreshComplete');

I tried as below but the id is not passing to the service(appealityApi),

var params = {
          id : $stateParams.id,
         }
        vm.loadList = function (forceRefresh,params) {
            appealityApi.getMixedMenu(forceRefresh,params).then(function (data) {
                vm.menuItems = data
            }).finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

});
        };

my service is as below ,

function getMixedMenu(forceRefresh,params) {
            $ionicLoading.show({ template: 'Loading...' })

        if (typeof forceRefresh === "undefined") { forceRefresh = false; }

        var deferred = $q.defer(),
            cacheKey = "basicCache",
            basicData = null;

        /*Grab from cache only if this is not a force refresh*/
        if (!forceRefresh) {
            basicData = basicDataCache.get(cacheKey);
            $ionicLoading.hide();
        }
        if (basicData) {
            console.log("Found data inside cache", basicData)
            deferred.resolve(basicData);
            $ionicLoading.hide();
        } else {
            $ionicLoading.show({ template: 'Loading...' })
                 $http.get(SERVER_URL + '/api/templates/getCategories?appId='+$rootScope.appId+'&mainId='+params.id)
                         .success(function(data) {
                             console.log('Received data via HTTP');
                                                      basicDataCache.put(cacheKey, data);
                                                      $ionicLoading.hide();
                                                      deferred.resolve(data);
                             console.log(data);
                         }).error(function(err) {
                             $ionicLoading.hide();
                             console.log('Error while making http call.');
                             deferred.reject();
                         });
        }
        return deferred.promise;
    };

The id returns undefined, I tried everything in the stack-overflow but nothing is working for me

6
  • var params = { id : $stateParams.id, } whats your $stateParams.id, did you try logging it? Commented Mar 2, 2016 at 6:49
  • Try to log $stateParams.id . I am sure $stateParams.id value undefined. Commented Mar 2, 2016 at 6:52
  • when I logged it I get the value of the id, ($stateParams 56d689f5e79f1c79311846bc). It is not undefined Commented Mar 2, 2016 at 6:55
  • okay so you are calling the loadlist function from the view right? can you show just the part where you call that funciton from the view Commented Mar 2, 2016 at 7:04
  • <ion-refresher pulling-text="Pull to refresh..."on-refresh="vm.loadList(true)"></ion-refresher><ion-list> <ion-item class="item-remove-animate item-thumbnail-left item-icon-right" ng-repeat="data in vm.menuItems" href="#/app/menu-detail/{{data._id}}" type="item-text-wrap"> <img ng-src="app/data/images/{{data.imageUrl}}">{{data.categoryName}} <br /> <p ng-show="{{data.is_recommended}}"><span class="gem-label warning">Chef Special</span></p><i class="icon ion-chevron-right icon-accessory"></i></ion-item></ion-list> Commented Mar 2, 2016 at 9:10

1 Answer 1

1

Try the following. See what you get error in log, and i will modify it as per your need.

var idRequired = $stateParams.id //log the $stateParams.id just incase
vm.loadList = function(forceRefresh) {
  appealityApi.getMixedMenu(
          forceRefresh, idRequired)
      .then(function(data) {
          vm.menuItems =
              data
      }).finally(function() {
          $scope.$broadcast(
              'scroll.refreshComplete'
          );
      });
  };

And, in Your Service:

function getMixedMenu(forceRefresh,
    idRequired) {
    console.log(idRequired); //make sure the idRequired is available.
    $ionicLoading.show({
        template: 'Loading...'
    })
    if (typeof forceRefresh ===
        "undefined") {
        forceRefresh = false;
    }
    var deferred = $q.defer(),
        cacheKey = "basicCache",
        basicData = null;
    /*Grab from cache only if this is not a force refresh*/
    if (!forceRefresh) {
        basicData = basicDataCache.get(
            cacheKey);
        $ionicLoading.hide();
    }
    if (basicData) {
        console.log(
            "Found data inside cache",
            basicData)
        deferred.resolve(basicData);
        $ionicLoading.hide();
    } else {
        $ionicLoading.show({
            template: 'Loading...'
        })
        $http.get(SERVER_URL +
            '/api/templates/getCategories?appId=' +
            $rootScope.appId +
            '&mainId=' + idRequired
        ).success(function(data) {
            console.log(
                'Received data via HTTP'
            );
            basicDataCache.put(
                cacheKey,
                data);
            $ionicLoading.hide();
            deferred.resolve(
                data);
            console.log(data);
        }).error(function(err) {
            $ionicLoading.hide();
            console.log(
                'Error while making http call.'
            );
            deferred.reject();
        });
    }
    return deferred.promise;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.