我正在尝试添加一项服务,以消除我通过我的应用程序重复的一些代码。
服务:
/* --------------------------------------------------------------
---------- FUNCTIONS FOR PAGE ALERTS THROUGHOUT Project-------
---------------------------------------------------------------*/
angular.module('app').service('PageAlertService', function () {
this.setAlert = function() {
console.log("In setAlert");
if (localStorage.getItem("Success")) {
var alertObj = {
alert: "Success",
alertMessage: localStorage.getItem("Success")
};
} else if (localStorage.getItem("Error"){
var alertObj = {
alert: "Error",
alertMessage: localStorage.getItem("Error")
};
};
return alertObj;
};
this.errorStatusCheck = function(error, successString) {
if (error.status = -1) {
localStorage.setItem("Success", successString);
} else {
localStorage.setItem("Error", "Error occured: " + error.status + error.statusText);
};
};
});
但每当我尝试将它添加到我的任何控制器时,我都想在它中使用它打破我的角度网络应用程序并给我
Error: $injector:unpr
Unknown Provider
这是我的app.js:
var app = angular.module('app',
[
'JobCtrl',
'JobSvc',
'WebsiteCtrl',
'WebsiteSvc',
'myClientCtrl',
'ClientSvc',
'MediaCompanyCtrl',
'MediaCompanySvc',
'PageAlertSvc',
'ui.bootstrap',
'ui.bootstrap.tpls'
]
);
这是我的控制器:
/* --------------------------------------------------
-------- Media Jobs Controller ----------------------
--------------------------------------------------- */
angular.module('app', ['ui.bootstrap', 'ui.bootstrap.tpls'])
.controller('JobCtrl',
[
'JobService',
'WebsiteService',
'MediaCompanyService',
'ProductService',
'$scope',
'$uibModal',
'PageAlertService',
function (JobService, WebsiteService, MediaCompanyService,
ProductService, $scope, $uibModal, $uibModalInstance, PageAlertService)
{
/* ------------------------------------
--------- Variables -----------------
-------------------------------------*/
$scope.mediaCompanies = {};
$scope.websites = {};
$scope.products = [];
$scope.selectedProducts = [];
$scope.isSelected = false;
$scope.new = {
Job: {}
};
/* ----------------------------------------------------------------
--- INITIALIZE LISTS OF JOBS, WEBSITES, COMPANIES, AND PRODUCTS ---
------------------------------------------------------------------*/
/* Get Jobs Initialization */
function getJobs() {
JobService.getJobs()
.then(
function (data) {
$scope.total_count = data.JobCount;
$scope.model = data.mediaJobList;
},
function (errResponse) {
console.log("Error while getting jobs");
});
};
getJobs();
/* Get Websites Initialization */
function getWebsites() {
WebsiteService.getWebsites()
.then(
function (data) {
$scope.websites = data;
},
function (errResponse) {
console.log(errResponse);
});
};
getWebsites();
/* Get Companies Initialization */
$scope.getCompanies = function () {
MediaCompanyService.getCompanies()
.then(
function (data) {
$scope.mediaCompanies = data;
},
function (errResponse) {
console.log(errResponse);
});
};
$scope.getCompanies();
/* Get Products -- passing website id*/
$scope.getProducts = function (webid) {
ProductService.getProducts(webid)
.then(
function (data) {
$scope.selectedProducts = data.MediaProductList;
},
function (errResponse) {
console.log(errResponse);
});
};
/* ----------------------------------------------------------------
----------- STILL NEED TO FIGURE OUT WHAT TO DO WITH YOU ----------
------------------------------------------------------------------*/
///* Shows Success and Error Alerts - Maybe make into a directive or
// Service? */
//if (localStorage.getItem("Success")) {
// $scope.alert = "Success";
// $scope.alertMessage = localStorage.getItem("Success");
// localStorage.clear();
//} else if (localStorage.getItem("Error") && localStorage.getItem("Error") !== null) {
// //sometimes get errors even when adding, deleting, updating is successful
// $scope.alert = "Error";
// $scope.alertMessage = localStorage.getItem("Error");
// localStorage.clear();
//};
if (localStorage.getItem("Success") != null || localStorage.getItem("Error") != null) {
console.log("I'm In")
var alert = {};
alert = PageAlertService.setAlert();
//$scope.alert = alert.alert;
//$scope.alertMessage = alert.alertMessage;
localStorage.clear();
}
/* -----------------------------------
------ JOB CRUD FUNCTIONS ----------
-------------------------------------*/
/* Add Job - Also Adds Products to Database */
$scope.addJob = function () {
var successString = "Added Job Succesfully!";
JobService.addJob($scope.new.Job).then(
function (success) {
localStorage.setItem("Success", successString);
},
function (error) {
//if (error.status = -1 && error.status !== 500) {
// localStorage.setItem("Success", successString);
//} else {
// localStorage.setItem("Error", "Error while adding Job! " + error.status + ":" + error.statusText);
//}
PageAlertService.errorStatusCheck(error, successString);
});
//adds products after adding job
addProducts();
location.reload();
}
/* Update Job -- Also Updates Products in Database */
$scope.updateJob = function (job) {
var successString = "Updated Job Succesfully!";
JobService.updateJob(job).then(
function (success) {
localStorage.setItem("Success", successString);
},
function (error) {
//if (error.status = -1 && error.status !== 500) {
// localStorage.setItem("Success", successString);
//} else {
// localStorage.setItem("Error", "Error while updating job! " + error.status + ":" + error.statusText);
//}
PageAlertService.errorStatusCheck(error, successString);
}
);
updateProducts();
location.reload();
}
/* Delete Job */
$scope.deleteJob = function (job) {
var successString = "Deleted Job Succesfully!";
var indx = $scope.model.indexOf(job);
JobService.deleteJob(job.Id).then(
function (success) {
localStorage.setItem("Success", successString);
},
function (error) {
//if (error.status = -1 && error.status !== 500) {
// localStorage.setItem("Success", successString);
//} else {
// localStorage.setItem("Error", "Error occured while deleting job! " + error.status + ":" + error.statusText);
//}
PageAlertService.errorStatusCheck(error, successString);
}
);
location.reload();
}
/* Run Job */
$scope.runJob = function (id, d1, d2) {
$scope.runButtonText = "Running";
//format Date
var date1 = $scope.FormatDate(d1);
var date2 = $scope.FormatDate(d2);
JobService.runJob(id, date1, date2).then(
function (success) {
$scope.runButtonText = "Finished";
},
function (error) {
if (error.status = -1 && error.status !== 500) {
$scope.runButtonText = "Finished";
} else {
$scope.runButtonText = "Error Occured";
console.log(error);
}
});
}
/* ----------------------------------------------------
---------- PRODUCT CRUD FUNCTIONS ---------------------
------------------------------------------------------*/
var addProducts = function () {
ProductService.addOrUpdate($scope.products).then(
function (response) {
console.log(response);
},
function (err) {
console.log(err);
});
};
var updateProducts = function () {
ProductService.addOrUpdate($scope.selectedProducts).then(
function (response) {
console.log(response);
},
function (err) {
console.log(err);
});
};
var deleteProducts = function (product) {
ProductService.deleteProduct(id).then(
function (response) {
console.log(response);
},
function (err) {
console.log(err);
});
};
/* ----------------------------------------------
--------- Code to Manipulate View Model --------
----------------------------------------------*/
/* Toggles Active Toggle Button */
$scope.updateActive = function (job) {
job.Active = !job.Active;
setTimeout(function () {
}, 500);
this.updateJob(job);
}
/* Selects Job and and Gets Product List */
$scope.selectJob = function (job) {
$scope.isSelected = true;
$scope.goToJobSection = false;
$scope.goToEditSection = true;
$scope.selectedJob = {}
$scope.selectedJob = job;
//selects product list by webid
var websiteId = $scope.selectedJob.WebsiteId;
$scope.getProducts(websiteId);
}
/* Onclick Event to stop people from
adding products to database with different
website ids*/
$scope.remProdOnClick = function () {
var newList = [];
if ($scope.goToEditSection == false) {
$scope.products = [];
}
}
/* ----------------------------------------------------------
--- MODAL -- May need to bring this out into a Directive ----
-----------------------------------------------------------*/
/* Shows Modal */
$scope.showModal = function (action, obj) {
$scope.$modalInstance = $uibModal.open({
scope: $scope,
inputs: {
title: action + " Job"
},
restrict: "E",
templateUrl: 'app/modal/JobModals/modal' + action + 'Template.html',
controller: "JobCtrl",
backdrop: 'static',
keyboard: false
});
}
/* Exits Modal */
$scope.exitModal = function () {
$scope.$modalInstance.dismiss('cancel');
};
}]);
我似乎无法弄清楚为什么会这样。我所有的其他服务都很完美。
谢谢!
答案 0 :(得分:2)
在你的控制器声明中,你注入7个依赖项,但你有8个作为参数,你要忘记注入一个依赖项或删除一个参数。
angular.module('app', ['ui.bootstrap', 'ui.bootstrap.tpls'])
.controller('JobCtrl',
[
'JobService',
'WebsiteService',
'MediaCompanyService',
'ProductService',
'$scope',
// missing dependency here
'$uibModal',
'PageAlertService',
function (JobService, WebsiteService, MediaCompanyService,
ProductService, $scope, $uibModal,
$uibModalInstance // or extra dependency here
, PageAlertService)
{
...
}
答案 1 :(得分:1)
我尝试了多种解决方案,但唯一可行的方法是将我的服务放入包含所有其他服务的文件夹中。一旦它被移动我不再得到我在上面讨论的$ injector错误。
/MainProject
-/app
-/Common
-/Controllers
JobCtrl.js
OtherCtrl.js
-/Services
JobSvc.js
PageAlertSvc.js
OtherSvs.js
-/Modal
-/Templates
-/Vendor
app.js
正如您所看到的,不是将PageAlertSvc.js放在Common中,而是将其放在Services文件夹中。
答案 2 :(得分:0)
在您的控制器中,您不需要重新定义模块(及其相关的依赖项),因为它已由app.js
定义。也许尝试将控制器连接到现有模块。
angular
.module('app')
.controller('JobCtrl', ...
说到app.js
,您不需要注入任何组件/控制器/服务,因为它们将在以后以编程方式附加。
angular.module('app', ['ui.bootstrap', 'ui.bootstrap.tpls']);