2016-04-24 94 views
3

我想注入我的服務我廠使用我廠在我的控制器:

控制器

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> 
+0

範圍哪些你期待你的'ProjectService'接受?服務 - 不像控制器和指令 - 沒有自己的範圍。也許你想要'$ rootScope'來代替? – Nick

回答

5

問題是,您在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'; 
    }; 
}]); 
相關問題