Javascript 将字符串值传递给Angluarjs中的$resource
我有以下的情况要完成 使用$resource我正在呼叫Javascript 将字符串值传递给Angluarjs中的$resource,javascript,angularjs,Javascript,Angularjs,我有以下的情况要完成 使用$resource我正在呼叫 App.factory("AppRepository", ['$resource', function ($resource) { return checkPerson: $resource('/api/accountAdd/:netName', { name: '@netName' }, { get: { method: 'GET' } }) }; }]); var netName="Tom
App.factory("AppRepository", ['$resource', function ($resource) {
return
checkPerson: $resource('/api/accountAdd/:netName', { name: '@netName' }, { get: { method: 'GET' } })
};
}]);
var netName="Tom"
AppRepository.checkPerson.get(netName, function (data) {
alert("success");
})
而不是从我的伙伴那里打电话
App.factory("AppRepository", ['$resource', function ($resource) {
return
checkPerson: $resource('/api/accountAdd/:netName', { name: '@netName' }, { get: { method: 'GET' } })
};
}]);
var netName="Tom"
AppRepository.checkPerson.get(netName, function (data) {
alert("success");
})
这不起作用,因为我正在传递netName的字符串。请让我知道如何将字符串值传递给工厂。我以前使用过id,它可以正常工作,但不确定如何处理字符串值。
谢谢应该是这样的
var netName="Tom"
AppRepository.checkPerson.get({ netName: value }, function (data) {
alert("success");
})
此外,您的工厂定义有错误,并且就在返回值之前,您的工厂返回未定义的
。使用return
将返回值移动到同一行
编辑:squid314上面的答案是正确的我相信您的
$resource
规范是不正确的。第二个参数是paramDefaults
hash。其中的键必须是URL中占位符的名称,值是指示如何填充占位符的字符串
App.factory("AppRepository", ['$resource', function ($resource) {
return {
checkPerson:
$resource('/api/accountAdd/:netName',
// key states that it will replace the ":netName" in the URL
// value string states that the value should come from the object's "netName" property
{ netName: '@netName' },
{ get: { method: 'GET' } })
};
}]);
这将允许您按如下方式使用服务:
var netName="Tom"
AppRepository.checkPerson.get({ netName: netName }, function (data) {
alert("success");
})
如果是
{netName:netName}
?$resource('/api/accountAdd/:netName',{netName:'@netName'}…也必须是固定的,我的意思是该值未定义。fwiw,散列键不需要引号。{netName:netName}
与{netName':netName}相同
你说得对,我只是在变量具有相同名称时引用键,是的{netName:netName}应该是…应该是{netName:netName}