角度服务错误

时间:2018-03-30 14:00:39

标签: angularjs angularjs-service

我有以下服务调用的代码实现,但是收到以下错误: " angular.min.js:102错误:[$ injector:unpr]"

请告知我以下错误。提前谢谢

    var app1 = angular.module('homeapp', []);
app1.service('APIService', function ($http) {
    this.getSkill = function (userid) {
        //return $http.get("api/GetSkillRating", )
    return $http({
                url: "api/GetSkillRating",
                method: "GET",
                params: { userid: userid }
            });
    }
});  

app1.controller("HomeController", ['$scope', 'APIService', function ($scope, APIService) {
    $scope.skillrating =[];

    $scope.getAll = function () {
        var servCall = APIService.getSkill(userid);
        servCall.then(function (d) {
            $scope.skillrating = d.data;
        }, function (error) {
                $log.error('Oops! Something went wrong while fetching the data.')
            })
    }  

}])

2 个答案:

答案 0 :(得分:1)

添加$ scope作为依赖

app1.controller("HomeController", ['$scope','APIService', function ($scope, APIService) {

答案 1 :(得分:0)

您无法在服务中使用$ scope 在控制器中,你必须注入$ scope和$ log

var app1 = angular.module('homeapp', []);

app1.service('APIService', function ($http) {
        this.getSkill = function () {
                //return $http.get("api/GetSkillRating", )
        return $http({
                                url: "api/GetSkillRating",
                                method: "GET",
                                params: { userid: $scope.userid }
                        });
        }
});

app1.controller("HomeController", ['$scope','$log','APIService', function ($scope, $log, APIService) {
        $scope.skillrating =[];
        getAll();

        function getAll() {
                var servCall = APIService.getSkill();
                servCall.then(function (d) {
                        $scope.skillrating = d.data;
                }, function (error) {
                                $log.error('Oops! Something went wrong while fetching the data.')
                        })
        }

}]);
相关问题