Jquery 是否可以在服务工厂()中以$http.get().success()格式返回数据?

Jquery 是否可以在服务工厂()中以$http.get().success()格式返回数据?,jquery,angularjs,Jquery,Angularjs,我已经用angular js创建了联系人列表。在下面的示例中,我创建了服务工厂,因为我无法处理服务中的$http.post().success()方法 angular.module('contactApp',['ngRoute'])) .config([“$routeProvider”, 函数($routeProvider){ $routeProvider。 当(“/contactList”{ templateUrl:'template/contactList.cfm', 控制器:“控制列表”

我已经用angular js创建了联系人列表。在下面的示例中,我创建了服务工厂,因为我无法处理服务中的$http.post().success()方法

angular.module('contactApp',['ngRoute']))
.config([“$routeProvider”,
函数($routeProvider){
$routeProvider。
当(“/contactList”{
templateUrl:'template/contactList.cfm',
控制器:“控制列表”
}).
当(“/contactAddEdit”{
templateUrl:'template/contactAddEdit.cfm',
控制器:“contactAddEdit”
}).
当(“/contactAddEdit/:contactID”{
templateUrl:'template/contactAddEdit.cfm',
控制器:“contactAddEdit”
}).
否则({
重定向到:“/contactList”
});
}
])
.controller('contList',函数($scope,studentSession){
studentSession.getSessions()成功(函数(数据、状态){
$scope.members==queryObject(数据);
});
})
.factory('studentSession',函数($http){
返回{
getSessions:function(){
返回$http.get('template/dbSelect.cfm');
}
};
});
您需要使用
then()
而不是
success()
您正在从工厂退回承诺:

studentSession.getSessions().then(function(data, status) {

    $scope.members = = queryToObject(data);

  }).catch(function (err) {

    console.log(err);
  });
如果您需要对IE8或android mobile<4.1等旧浏览器提供更多支持,请使用以下选项:

studentSession.getSessions().then(function(data, status) {

        $scope.members = = queryToObject(data);

      },function (err) {

        console.log(err);
      });
然后如果您真的想从工厂退货,您需要一个活动:

.controller('contList', function($scope, studentSession) {

  studentSession.getSessions();

  $scope.$on('session:retrievedSession', function (data) {

    console.log('Session retrieved is: ', data.response);
  });
})

.factory('studentSession', function($rootScope, $http) {

  return {

    getSessions: function() {

      return $http.get('template/dbSelect.cfm').success(function (response) {

        $rootScope.$broadcast('session:retrievedSession', {response:response});
      });

    }

  };
});

请注意,此
.catch
将在IE8中引发错误。您可以使用
的第二个参数来处理错误。或者,如果您确实想使用
catch
,则必须将其写成
[“catch”]
@JonSnow;)是的,Android mobile也一样<4.1 afaik:)有可能在工厂中返回success()吗?或者我没有得到你想要的东西need@sbaaaang我无法捕获事件$rootScope。$broadtcast(),您在工厂()中注入了$rootScope,但没有注入$broadtcast为什么?。谢谢你花宝贵的时间陪我。