AngularJs从$ resource检索数据

时间:2014-10-08 03:50:17

标签: angularjs callback

我有一项提供

的服务
services.factory('proService', ['$resource', function ($resource) {
    return {
        getValue: function (context_params) {
            var Pro = $resource('pro');
            return Pro.get({user_id: context_params.userId, course_id: context_params.cKey, activity_id: context_params.activityId});
        }
    };
}]);

我的控制器代码。但我只是把它作为一个承诺。我需要回电话来获得熟练程度值。

$scope.getPro = function () {
        $scope.pro = proService.getValue({userId: $scope.userId, courseKey: $scope.courseKey, activityId: $scope.activityId});
        $scope.pro.rating = 5 * Math.round(($scope.pro.pro * 10)/5);
        console.log($scope.pro);
    };

$ scope.pro给了我

f {$promise: Object, $resolved: false, $get: function, $save: function, $query:    function…}
$promise: Object
 >catch:function(callback){
 >finally:function(callback){
 >then:function(callback,errback,progressback){
 >_proto_:object
 $resolved: true
 proficiency: 2
 >profs:object
 >_proto_:Resource

我无法调用pro的值,即" 2"。这是我在控制器中尝试的内容

$scope.getPro = function () {
        $scope.pro = proService.getValue($scope.userId, $scope.snapshotId).then(function(data) {
             $scope.rating = data.pro;
             console.log($scope.rating);
             });
        };

请帮助我解决我在这里做错的事。感谢

1 个答案:

答案 0 :(得分:0)

您需要重写控制器代码以处理从您的服务返回的承诺。试试这个:

$scope.getProficiency = function () {
        proficiencyService.getValue({userId: $scope.userId, courseKey: $scope.courseKey, activityId: $scope.activityId}).
    then(function(proficiencyValue) {
        $scope.proficiency = proficiencyValue;
        $scope.proficiency.rating = 5 * Math.round(($scope.proficiency.proficiency * 10)/5);
        console.log($scope.proficiency);
    });
};