编程语言
首页 > 编程语言> > javascript – 在出厂错误中注入服务:[$injector:unpr]未知提供者:$scopeProvider < - $scope < - ProjectService

javascript – 在出厂错误中注入服务:[$injector:unpr]未知提供者:$scopeProvider < - $scope < - ProjectService

作者:互联网

我想在我的工厂注入我的服务,在我的控制器中使用我的工厂:

调节器

app.home.controller('HomeController', ['$scope', '$http', 'Project', function ($scope, $http, Project) {

    var project = new Project();

    $scope.refresh = function(){
        project.createTable();
    };

    $scope.refresh();

}]);

型号(工厂)

app.project.factory('Project', function (ProjectService) {

    var Project = function (properties) {
        // Model
        this.file    = null;
        this.name      = null;
        this.path  = null;
        this.is_active = null;

        angular.extend(this, properties);
    };

    Project.prototype.setModel = function (obj) {
        angular.extend(this, obj);
    };

    Project.prototype.createTable = function () {
        console.log(this);
        return ProjectService.ok();
    };

    return Project;

});

服务

app.project.service('ProjectService', ['$scope', '$http', function ($scope, $http) {

    this.ok = function() {
      return 'all';  
    };

}]);

但是我有一个错误:

angular.min.js:13550 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- ProjectService <- Project

我没有看到我的错误..

我试图重命名模型/服务它是同样的错误

在我的index.html中:

    <!--Project-->
    <script src="js/modules/project/project.js"></script>
    <script src="js/modules/project/model/project.model.js"></script>
    <script src="js/modules/project/service/project.service.js"></script>

解决方法:

问题是,您在ProjectService服务中注入了$scope提供程序.

You can not inject $scope provider in service, basically it can be
available to inject in controller & directive link function only.

app.project.service('ProjectService', ['$http', function ($http) {
   var self = this;
    self.ok = function() {
      return 'all';  
    };
}]);

标签:javascript,angularjs,angularjs-service
来源: https://codeday.me/bug/20190608/1200582.html