Javascript 无法连接angularjs中的服务功能
我有以下代码:Javascript 无法连接angularjs中的服务功能,javascript,angularjs,undefined,Javascript,Angularjs,Undefined,我有以下代码: 服务: (功能(){ "严格使用",; 有棱角的 .module('myApp') .工厂(“服务工厂”,服务工厂); ServiceFactory.$inject=['$http','$rootScope','serviceURLHostFactory','$q']; 变量配置={ //要做的事情:检查是否需要任何东西 }; /*@ngInject*/ 函数ServiceFactory($http、$rootScope、serviceURLHostFactory、$q){ v
服务:
(功能(){
"严格使用",;
有棱角的
.module('myApp')
.工厂(“服务工厂”,服务工厂);
ServiceFactory.$inject=['$http','$rootScope','serviceURLHostFactory','$q'];
变量配置={
//要做的事情:检查是否需要任何东西
};
/*@ngInject*/
函数ServiceFactory($http、$rootScope、serviceURLHostFactory、$q){
var会话={};
var userName=null;
var userId=null;
var pwd=null;
函数getUserSession(){
返回会议;
}
函数setUserSession(会话){
会话=会话;
}
函数getUserName(){
返回用户名;
}
函数setUserName(用户名){
用户名=用户名;
}
函数getUserId(){
返回用户标识;
}
函数setUserId(userId){
userId=userId;
}
函数getPwd(){
返回pwd;
}
功能设置pwd(pwd){
pwd=pwd;
}
var服务={
getUserSession:getUserSession,
setUserSession:setUserSession,
getUserName:getUserName,
setUserName:setUserName,
getUserId:getUserId,
setUserId:getUserId,
getPwd:getPwd,
setPwd:setPwd,
getLoginSession:getLoginSession
};
var响应={};
//获取使用会话
函数getLoginSession(){
var URL=serviceURLHostFactory.getServiceServerHost();
/*返回$http({
方法:“POST”,
url:'response.json',
数据:{'username':getUserName,'pwd':getPwd}
})
。然后(httpCompleted)
.catch(httpFailed)*/
响应=用户“:{”uid“:”3”,“名称“:”xyz”,“邮件“:”test23@localhost.com“,”主题“:”签名“:”签名“:”格式“:”过滤的“:”html“,”创建“:”146242963“,”访问“:”1462445668“,”登录“:”1462445753“,”状态“:”1“,”时区“:”亚洲/加尔各答“,”语言“,”图片“:”空,“数据“:”假“,”角色“:{”2“:”认证用户“,”rdf映射“:{”rdftype“:”sioc:UserAccount“],“名称”:{“谓词”:[“foaf:name”]},主页:{“谓词”:[“foaf:page”],“类型”:“rel”}}
返回响应;
功能httpCompleted(响应){
返回响应数据;
}
函数httpFailed(e){
console.error('无法登录'+JSON.stringify(e));
返回$q.reject(e);
}
}
返回响应;
}
})();
看起来您的工厂服务没有公开cotroller或其他服务将使用的所有公共方法;例如setUserSession、getLoginSession等。我看到还有其他问题,但未定义的错误不是一个函数,因为您没有向外界公开这些方法
为此,您的服务必须遵循以下模式:
(function () {
'use strict';
angular
.module('myApp')
.factory('ServiceFactory', ServiceFactory);
ServiceFactory.$inject = ['$http', '$rootScope', 'serviceURLHostFactory','$q'];
var config = {
// TO DO : check if anything required
};
/* @ngInject */
function ServiceFactory($http, $rootScope, serviceURLHostFactory, $q) {
var session = {};
var userName = null;
var userId = null;
var pwd = null;
function getUserSession() {
return session;
}
function setUserSession(session) {
session = session;
}
function getUserName() {
return userName;
}
function setUserName(userName) {
userName = userName;
}
function getUserId() {
return userId;
}
function setUserId(userId) {
userId = userId;
}
function getPwd() {
return pwd;
}
function setPwd(pwd) {
pwd = pwd;
}
return {
getUserSession: getUserSession,
setUserSession: setUserSession,
getUserName: getUserName,
setUserName: setUserName,
getUserId: getUserId,
setUserId: getUserId,
getPwd:getPwd,
setPwd:setPwd,
getLoginSession: getLoginSession
};
}
})();
看起来您的工厂服务尚未公开cotroller或其他服务将使用的所有公共方法;例如setUserSession、getLoginSession等。我看到还有其他问题,但未定义的错误不是一个函数,因为您尚未向外界公开这些方法 为此,您的服务必须遵循以下模式:
(function () {
'use strict';
angular
.module('myApp')
.factory('ServiceFactory', ServiceFactory);
ServiceFactory.$inject = ['$http', '$rootScope', 'serviceURLHostFactory','$q'];
var config = {
// TO DO : check if anything required
};
/* @ngInject */
function ServiceFactory($http, $rootScope, serviceURLHostFactory, $q) {
var session = {};
var userName = null;
var userId = null;
var pwd = null;
function getUserSession() {
return session;
}
function setUserSession(session) {
session = session;
}
function getUserName() {
return userName;
}
function setUserName(userName) {
userName = userName;
}
function getUserId() {
return userId;
}
function setUserId(userId) {
userId = userId;
}
function getPwd() {
return pwd;
}
function setPwd(pwd) {
pwd = pwd;
}
return {
getUserSession: getUserSession,
setUserSession: setUserSession,
getUserName: getUserName,
setUserName: setUserName,
getUserId: getUserId,
setUserId: getUserId,
getPwd:getPwd,
setPwd:setPwd,
getLoginSession: getLoginSession
};
}
})();
你能在其他问题上指导我吗@rout你能在其他问题上指导我吗@rout