Javascript 未知提供程序factoryprovider<;-工厂<;-控制器角度js
我在将依赖项从服务注入控制器时遇到问题。虽然我添加了它,但仍然有相同的错误Javascript 未知提供程序factoryprovider<;-工厂<;-控制器角度js,javascript,angularjs,Javascript,Angularjs,我在将依赖项从服务注入控制器时遇到问题。虽然我添加了它,但仍然有相同的错误 未知提供程序:websiteFactoryProvider当您的工厂未向angular注册时,会出现此错误。换成 app.factory('websiteFactory', ['$http', '$location', function ($http, $location) { ...do something here... } 当您的工厂未向angular注册时,会出现此错误。换成 app.factory('
未知提供程序:websiteFactoryProvider当您的工厂未向angular注册时,会出现此错误。换成
app.factory('websiteFactory', ['$http', '$location', function ($http, $location) {
...do something here...
}
当您的工厂未向angular注册时,会出现此错误。换成
app.factory('websiteFactory', ['$http', '$location', function ($http, $location) {
...do something here...
}
您拼错了“websiteFactory”。在您的工厂定义代码中,它是“websiteFactory”,但在控制器中,您使用“websiteFactory”以不同的名称获取它,这就是它无法找到此提供程序并产生错误的原因:
更改:
app.factory('websiteFactory',['$http','$location',函数($http,$location){
变量工厂={};
//从http请求返回所有网站的方法
factory.getAllWebsites=函数(){
返回$http.get(“http://localhost/Replicate/GetAllWebsites");
}
//方法,该方法从给定数组返回对象
factory.getFilteredObject=函数(列表、网站名){
对于(i=0;i
您拼错了“websiteFactory”。在您的工厂定义代码中,它是“websiteFactory”,但在控制器中,您使用“websiteFactory”以不同的名称获取它,这就是它无法找到此提供程序并产生错误的原因:
更改:
app.factory('websiteFactory',['$http','$location',函数($http,$location){
变量工厂={};
//从http请求返回所有网站的方法
factory.getAllWebsites=函数(){
返回$http.get(“http://localhost/Replicate/GetAllWebsites");
}
//方法,该方法从给定数组返回对象
factory.getFilteredObject=函数(列表、网站名){
对于(i=0;i
工厂名称写错了<代码>网站工厂!=<代码>网站服务只是一个输入错误;)“webiteFactory”你忘了“S”是的,你是对的@Foubert。谢谢你纠正了这个愚蠢的问题。你把厂名弄错了<代码>网站工厂!=<代码>网站服务只是一个输入错误;)“webiteFactory”你忘了“S”是的,你是对的@Foubert。谢谢你纠正了这个愚蠢的问题。上面的评论有什么不同?它不是已经注册了吗?与上面的评论有什么不同?它不是已经注册了吗?
app.factory('webiteFactory', ['$http', '$location', function ($http, $location) {
var factory = {};
// method that return all websites from an http request
factory.getAllWebsites = function () {
return $http.get("http://localhost/Replicate/GetAllWebsites");
}
//method that returns an object from given array
factory.getFilteredObject = function (list, websiteName) {
for (i = 0 ; i < list.length ; i++) {
if (list[i].Name == websiteName)
return list[i];
}
}
return factory;
}]);
/* application services that would not return values */
app.service('websiteService', ['$http', function ($http) {
//service for pagination
this.paginate = function ($scope) {
//pagination code
$scope.currentPage = 1;
$scope.totalItems = $scope.model.length;
$scope.numPerPage = 10;
$scope.paginate = function (value) {
var begin, end, index;
begin = ($scope.currentPage - 1) * $scope.numPerPage;
end = begin + $scope.numPerPage;
index = $scope.model.indexOf(value);
return (begin <= index && index < end);
};
//ordering code
$scope.reverse = true;
$scope.order = function (predicate) {
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
$scope.predicate = predicate;
};
}
//service to change state of a website
this.changeSiteState = function (website) {
var newState = website.State == "Stopped" ? "Started" : "Stopped";
$http({
method: 'POST',
url: '/Replicate/ChangeState/',
data: JSON.stringify({ webName: website.Name, state: newState }),
headers: { 'Content-Type': 'application/json' }
}).success(function (data) {
if (data == "success") {
website.State = website.State == "Stopped" ? "Started" : "Stopped";
}
else {
alert(data);
}
}).error(function (data, status, headers, config) {
alert(data);
});
}
}])
app.controller('listCtrl', function websiteCtrl($scope, $location, websiteFactory, websiteService, $modal) {
//function triggered at the intialization of controller
$scope.init = function () {
$scope.model = [];
setTimeout(function () {
websiteFactory.getAllWebsites().success(function (data) {
$scope.model = data;
websiteService.paginate($scope);
})
}, 0);
};
//delegation of change state to service method
$scope.changeState = function (website) {
websiteService.changeSiteState(website);
}
//open modal and shows details of single object
$scope.showDetails = function (websiteName) {
var modalInstance = $modal.open({
templateUrl: '../Views/Replicate/Details.html',
controller: 'DetailsCtrl',
resolve: {
obj: function () {
return websiteFactory.getFilteredObject($scope.model, websiteName);
}
}
});
}
});
app.factory('websiteFactory', ['$http', '$location', function ($http, $location) {
...do something here...
}
app.factory('websiteFactory', ['$http', '$location', function ($http, $location) {
var factory = {};
// method that return all websites from an http request
factory.getAllWebsites = function () {
return $http.get("http://localhost/Replicate/GetAllWebsites");
}
//method that returns an object from given array
factory.getFilteredObject = function (list, websiteName) {
for (i = 0 ; i < list.length ; i++) {
if (list[i].Name == websiteName)
return list[i];
}
}
return factory;
}]);