如何从控制器向angularjs中的服务发送两个参数

时间:2016-03-02 06:45:31

标签: angularjs parameters factory controllers

我想发送$stateparams.id以及下面的内容,

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>

控制器:

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

我尝试如下,但ID没有传递给服务(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');
            });
        };

});
        };

我的服务如下所示,

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;
    };

id返回undefined,我尝试了堆栈溢出中的所有内容,但没有什么对我有用

1 个答案:

答案 0 :(得分:1)

  

尝试以下方法。查看日志中出现的错误,我将对其进行修改   根据您的需要。

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'
          );
      });
  };

并且,在服务

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;
};