我似乎不明白为什么我不能在我的 Controller 中加载我的 AuthFactory 工厂。语法对我来说看起来是正确的。另请注意,portalAPI 工厂的构建方式与 AuthFactory 相同,但它被正确注入(inject)。如果有人可以帮助我,我将不胜感激:
(function() {
var app = angular.module('contactAuthApp');
//////////////////////////
// BEGIN - CONTROLLER
//////////////////////////
var ContactAuthController = function($rootScope, $scope, $stateParams, $state, $timeout, portalAPI, $location, AuthFactory) {
$scope.resetcode = $location.hash();
$scope.loggedIn = false;
$scope.errormsgheader;
$scope.errormsgbody;
$scope.successmsgheader;
$scope.successmsgbody;
$scope.errDialogHidden = true;
$scope.successDialogHidden = true;
var onUserComplete = function(response) {
for (var i in response.contact) {
var obj = response.contact[i];
$rootScope.user = obj;
}
$rootScope.companies = response.companies;
//$rootScope.databases = response.databases;
for (var i in response.databases) {
var obj = response.databases[i];
//$scope.secondaryData.push(obj);
$rootScope.databaseName = obj.databasename;
}
for (var i in $rootScope.companies) {
var obj = $rootScope.companies[i];
//$scope.secondaryData.push(obj);
$rootScope.companyID = obj.ID;
$rootScope.companyOptions = [{
name: obj.name,
id: obj.ID
}];
}
$scope.loggedIn = true;
portalAPI.getCompanyBrands($rootScope.companyID, 'active').success(goTest).error(goCharts);
};
var onForgotPasswordComplete = function(response) {
/* Show/hide windows and alerts */
$scope.errDialogHidden = true;
$scope.successDialogHidden = false;
$scope.successmsgheader = response.success.heading;
$scope.successmsgbody = response.success.body;
$timeout(function() {
$state.go('/');
}, 4000);
};
var goWelcome = function(response) {
$state.go('welcome');
//$state.go("welcome");
var data = response.companybrands;
$rootScope.brandOptions = [];
for (var i in data) {
var obj = {
name: data[i].Name,
id: data[i].ID
};
$rootScope.brandOptions.push(obj);
}
};
var goTest = function() {
var g = AuthFactory.isAuthenticated();
//$state.go("testing");
}
var goCharts = function() {
$state.go("chart");
}
var goDashboard = function() {
$state.go('dashboard');
}
var onError = function(response) {
var error = response.error;
$scope.errDialogHidden = false;
$scope.errormsgheader = error.heading;
$scope.errormsgbody = error.body;
$scope.successDialogHidden = true;
};
$scope.authenticate = function() {
portalAPI.getUser($scope.username, $scope.password).success(onUserComplete).error(onError);
};
$scope.newPassRequest = function() {
portalAPI.getForgotPassword($scope.username).success(onForgotPasswordComplete).error(onError);
};
$scope.newPassRequestVerify = function() {
portalAPI.getForgotPasswordVerify($scope.username, $scope.resetcode, $scope.newpassword, $scope.confirmnewpassword).success(onForgotPasswordComplete).error(onError);
};
$scope.submitparameters = function() {
$rootScope.startDate = $scope.startDate;
$rootScope.endDate = $scope.endDate;
$rootScope.clientdb = $rootScope.databaseName;
$rootScope.companyid = $scope.companyOptions.id.id;
$rootScope.brandid = $scope.brandOptions.id.id;
// The number of milliseconds in one day
var ONE_DAY = (1000 * 60 * 60 * 24);
// Convert both dates to milliseconds
var date1_ms = Date.parse($rootScope.startDate)
var date2_ms = Date.parse($rootScope.endDate)
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
$rootScope.diffDays = Math.round(difference_ms / ONE_DAY)
$state.go('dashboard');
}
};
//////////////////////////
// END - CONTROLLER
//////////////////////////
/////////////////////////
// BEGIN - FACTORIES
/////////////////////////
var portalAPI = function($http) {
var getUser = function(username, password) {
return $http({
url: "https://staging.revguard.net/portalapi/index.php",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
responseType: 'json',
data: {
"authentication": [{
"apiusername": "jaime",
"apipassword": "summit",
"apicompanyid": 3
}],
"controller": "Contact",
"method": "contactAuthenticate",
"params": [{
"username": username,
"password": password
}]
}
})
.success(function(response) {
//console.log("success"); console.log(response)
return response;
})
.error(function(response) {
//console.log("error"); console.log(response);
return response;
});
};
var getCompanyBrands = function(companyid, status) {
return $http({
url: "https://staging.revguard.net/portalapi/index.php",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
responseType: 'json',
data: {
"authentication": [{
"apiusername": "jaime",
"apipassword": "summit",
"apicompanyid": 3
}],
"controller": "CompanyBrand",
"method": "getCompanyBrands",
"params": [{
"companyid": companyid,
"status": status
}]
}
})
.success(function(response) {
return response;
})
.error(function(response) {
return response;
});
};
var getForgotPassword = function(username) {
return $http({
url: "https://staging.revguard.net/portalapi/index.php",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
responseType: 'json',
data: {
"authentication": [{
"apiusername": "jaime",
"apipassword": "summit",
"apicompanyid": 3
}],
"controller": "Password",
"method": "forgotPasswordTrigger",
"params": [{
"username": username
}]
}
})
.success(function(response) {
return response;
})
.error(function(response) {
return response;
});
};
var getForgotPasswordVerify = function(username, resetcode, newpassword, verifynewpassword) {
return $http({
url: "https://staging.revguard.net/portalapi/index.php",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
responseType: 'json',
data: {
"authentication": [{
"apiusername": "jaime",
"apipassword": "summit",
"apicompanyid": 3
}],
"controller": "Password",
"method": "forgotPasswordVerify",
"params": [{
"username": username,
"resetcode": resetcode,
"newpassword": newpassword,
"verifypassword": verifynewpassword
}]
}
})
.success(function(response) {
return response;
})
.error(function(response) {
return response;
});
};
return {
getUser: getUser,
getCompanyBrands: getCompanyBrands,
getForgotPassword: getForgotPassword,
getForgotPasswordVerify: getForgotPasswordVerify
};
};
var AuthFactory = function($scope, $rootScope, $state, $stateParams) {
var authToken = false;
var isAuthenticated = function() {
return authToken;
};
var login = function(state) {
//return (typeof(state) == 'string') ? $state.go(state) : $state.go("route1");
};
var logout = function() {
alert("You've been logged out");
$state.go("route1");
};
return {
isAuthenticated: isAuthenticated,
login: login,
logout: logout
};
};
////////////////////////
// END - FACTORIES
////////////////////////
////////////////////////
// BEGIN - DIRECTIVES
////////////////////////
////////////////////////
// END - DIRECTIVES
////////////////////////
app.controller("ContactAuthController", ContactAuthController);
app.factory("portalAPI", portalAPI);
app.factory("AuthFactory", AuthFactory);
})();
最佳答案
你不能将 $scope
注入(inject)工厂,因为没有工厂。尝试 var AuthFactory = function($rootScope, $state, $stateParams) {
并使用 $rootScope
而不是 $scope
或传递范围从 controller
到您的服务。
https://docs.angularjs.org/guide/di
$scope: Controllers are always associated with a point in the DOM and so are provided with access to the scope at that point. Other components, such as services only have access to the singleton $rootScope service.
关于javascript - 为什么我的工厂出现喷油器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961182/