I'm facing the problem on passing the argument from controller to service module
我正面临将参数从控制器传递到服务模块的问题
I don't know where I have pass the arguments in controller module and how to get the value in service module. here is my code,
我不知道我在哪里传递控制器模块中的参数以及如何获取服务模块中的值。这是我的代码,
Controller module
var user = $scope.username;
var pass = $scope.password;
*// how to pass the username and password here*
checkStatus.query(function(response, headers) {
alert(headers('X-Internal-Auth-Toketn'));
});
Service module
UIAppResource.factory('checkStatus', function($resource) {
var auth = Base64.encode("abcd:abcd"); // *I need username and password here*
return $resource(baseURL + "status", {},
{
'query': {
method: 'GET',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic '+ auth,
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
isArray: false
}
}
)
});
I'm new for angularjs, please help me to solve this problem
我是angularjs的新手,请帮我解决这个问题
Thanks in advance
提前致谢
2 个解决方案
#1
2
Rather than return $resource
, return an object that contains functions that use the $resource
internally. Eg:
而不是返回$ resource,返回一个包含内部使用$ resource的函数的对象。例如:
Controller:
var user = $scope.username;
var pass = $scope.password;
checkStatus.customQuery(user, pass, function(response, headers) {
alert(headers('X-Internal-Auth-Toketn'));
});
Service:
UIAppResource.factory('checkStatus', function($resource) {
return {
customQuery: function(user, pass, callback) {
var auth = Base64.encode(user + ':' + pass);
var myRes = $resource(baseURL + "status", {}, {
'query': {
method: 'GET',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic '+ auth,
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
isArray: false
}
});
myRes.query(callback);
}
};
});
Obviously this will create a new $resource for every customQuery()
call, so it is most likely better to store the created $resource inside the service once. To do this I would create an initialiser function which the username/password can be passed to.
显然,这将为每个customQuery()调用创建一个新的$ resource,因此最好将创建的$ resource存储在服务中一次。为此,我将创建一个初始化函数,用户名/密码可以传递给它。
#2
0
You should restructure your factory definition that makes easier for your to pass parameters. CheckStatus
should be a method on the object returned from factory The definition should look something like
您应该重新构建工厂定义,以便更容易传递参数。 CheckStatus应该是从工厂返回的对象的方法。定义应该类似于
UIAppResource.factory('authService', function($resource) {
var service={};
service.checkStatus=function(userName,passWord) {
var auth = Base64.encode("abcd:abcd"); // *I need username and password here*
return $resource(baseURL + "status", {},
{
'query': {
method: 'GET',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic '+ auth,
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
isArray: false
}
}
)
}
return service;
});
In you controller then you call
在你的控制器然后你打电话
authService.checkStatus($scope.userName,$scope.password);
#1
2
Rather than return $resource
, return an object that contains functions that use the $resource
internally. Eg:
而不是返回$ resource,返回一个包含内部使用$ resource的函数的对象。例如:
Controller:
var user = $scope.username;
var pass = $scope.password;
checkStatus.customQuery(user, pass, function(response, headers) {
alert(headers('X-Internal-Auth-Toketn'));
});
Service:
UIAppResource.factory('checkStatus', function($resource) {
return {
customQuery: function(user, pass, callback) {
var auth = Base64.encode(user + ':' + pass);
var myRes = $resource(baseURL + "status", {}, {
'query': {
method: 'GET',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic '+ auth,
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
isArray: false
}
});
myRes.query(callback);
}
};
});
Obviously this will create a new $resource for every customQuery()
call, so it is most likely better to store the created $resource inside the service once. To do this I would create an initialiser function which the username/password can be passed to.
显然,这将为每个customQuery()调用创建一个新的$ resource,因此最好将创建的$ resource存储在服务中一次。为此,我将创建一个初始化函数,用户名/密码可以传递给它。
#2
0
You should restructure your factory definition that makes easier for your to pass parameters. CheckStatus
should be a method on the object returned from factory The definition should look something like
您应该重新构建工厂定义,以便更容易传递参数。 CheckStatus应该是从工厂返回的对象的方法。定义应该类似于
UIAppResource.factory('authService', function($resource) {
var service={};
service.checkStatus=function(userName,passWord) {
var auth = Base64.encode("abcd:abcd"); // *I need username and password here*
return $resource(baseURL + "status", {},
{
'query': {
method: 'GET',
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization': 'Basic '+ auth,
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
isArray: false
}
}
)
}
return service;
});
In you controller then you call
在你的控制器然后你打电话
authService.checkStatus($scope.userName,$scope.password);