我似乎无法弄清楚为什么我无法在控制器中加载AuthFactory工厂。语法对我来说是正确的。另请注意,portalAPI工厂的构建方式与AuthFactory相同,无论它是如何正确注入的。如果有人可以帮助我,我会非常感激:
(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);
})();
答案 0 :(得分:2)
您无法将$scope
注入工厂,因为没有。尝试使用var AuthFactory = function($rootScope, $state, $stateParams) {
并使用$rootScope
代替$scope
,或将范围从controller
传递给您的服务。
https://docs.angularjs.org/guide/di
$ scope:控制器始终与DOM中的某个点相关联,因此也是如此 提供了对该范围的访问权限。其他组件, 例如服务只能访问singleton $ rootScope服务。