2014-03-26 40 views
0

我有以下情况想完成

使用$资源我打电话

App.factory("AppRepository", ['$resource', function ($resource) { 
     return 
     checkPerson: $resource('/api/accountAdd/:netName', { name: '@netName' }, { get: { method: 'GET' } }) 
    }; 
}]); 

比我cotroller我打电话

var netName="Tom" 
    AppRepository.checkPerson.get(netName, function (data) { 
     alert("success"); 
    }) 

这没有工作,因为我传递的字符串为netName。请让我知道如何将字符串值传递给工厂。我以前用id工作过,它工作正常,但不知道如何处理字符串值。 感谢

回答

1

我相信你$resource规格不正确。第二个参数是paramDefaults散列。其中的键必须是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"); 
}) 
1

应该是这样

var netName="Tom" 
    AppRepository.checkPerson.get({ netName: value }, function (data) { 
     alert("success"); 
    }) 
+0

应该说是'{NETNAME:NETNAME}'? – Shanimal

+0

{“netName”:netName},在这种情况下... – roboli

+0

$ resource('/ api/accountAdd /:netName',{** netName **:'@netName'} ...必须修复 – roboli

0
AppRepository.checkPerson.get({name: netName}, function (data) { 
    alert("success"); 
}) 

而且,你的工厂定义窃听,you cannot start a new line after a return和返回值之前吧,你的工厂返回undefined。将您的返回值与return在同一行中移动。

编辑:以上squid314的答案是正确的

+0

应该是{netName:netName} – Shanimal