gpt4 book ai didi

javascript - 在工厂错误 : [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- ProjectService <- Project 中注入(inject)服务

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:09 25 4
gpt4 key购买 nike

我想在我的工厂中注入(inject)我的服务,以便在我的 Controller 中使用我的工厂:

Controller

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 服务中注入(inject)了 $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 - 在工厂错误 : [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- ProjectService <- Project 中注入(inject)服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36825095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com