Angular $ http问题和承诺问题

时间:2016-07-23 08:53:07

标签: angularjs ionic-framework

我无法在我的模态中传递Id,这使我无法传递数据

Controller.js

app.controller('faq', function ($scope, faqservice, $ionicModal) {
  $scope.faq = faqservice;
  console.log($scope.faq); //upto this everything i working properly and i am able to render in my HTML page ! 

  $ionicModal.fromTemplateUrl('templates/faqDetails.html', {
    scope: $scope
  }).then(function (modal) {
    $scope.faqDetails = modal;
  });

  // Triggered in the FAQ detail modal to close it
  $scope.faqClose = function () {
    $scope.faqDetails.hide();
  };
  // Open the FAQ detail modal
  $scope.faqOpen = function (id) {
    $scope.faqDetails.show();
    $scope.notes = faqservice.getid(id);
    console.log($scope.notes); //**i am geting this null.. and when i console.log() in getID method in service my for loop is not executing **
  };
});

Service.js

app.service("faqservice", function ($q, $http,Events,$ionicPopup) {
  var self = {
    'results': [],
    getid: function (id) {
      for (var i = 0; i < self.results.length; i++) {
        if (self.results[i].id === parseInt(id)) {
          return self.results[i];
        }
      }
      return null;
    },
    'load': function () {
      $http.get(Events.url +"/faqs")
        .then(function (response) {
          angular.forEach(response.data, function (data) {
            self.results.push(data);
            window.localStorage.setItem("faqs", JSON.stringify(data));
          });
        }
        ,function (data) {
          $ionicPopup.alert({
            title: 'Slow Internet connection',
            template: 'Please check your internet connection for updates !'
          });
          if (window.localStorage.getItem("faqs") !== undefined) {
            self.results.push(JSON.parse(window.localStorage.getItem("faqs")));
          }
        });

    }
  };
  self.load();

  return self;
});

*当我的互联网连接关闭时,我的错误回调无效!我的互联网连接关闭时没有收到错误通知*

I am Getting My $scope.notes null pls help me with this issue !!

and  I am really new to Angular.js and ionic so can u pls suggest me what to 

在使用HTTP时使用success()或then()?

1 个答案:

答案 0 :(得分:1)

如果您在console.log(self.results.length)循环之前for怎么办?

我认为success()的{​​{1}}方法已被弃用。