0

I have following situation trying to accomplish

Using $resource I am calling

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

Than from my cotroller I am calling

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

This doesnt work, because I am passing string for netName. Please let me know how can I pass string value to the factory. I have worked previously with id and it works fine but not sure how to do with string value. Thanks

J. Davidson
  • 3,297
  • 13
  • 54
  • 102

3 Answers3

1

Should be like this

var netName="Tom"
    AppRepository.checkPerson.get({ netName: value }, function (data) {
        alert("success");
    })
roboli
  • 1,418
  • 20
  • 24
1

I believe your $resource specification is incorrect. The second parameter is the paramDefaults hash. The keys in that must be the names of placeholders in your URL and the values are strings indicating how to fill in the placeholder.

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' } })
    }; 
}]);

This should allow you to use the service as follows:

var netName="Tom"
AppRepository.checkPerson.get({ netName: netName }, function (data) {
    alert("success");
})
squid314
  • 1,394
  • 8
  • 9
0
AppRepository.checkPerson.get({name: netName}, function (data) {
    alert("success");
})

Also, your factory definition is bugged, you cannot start a new line after a return and right before the return value, your factory returns undefined. Move your return value in the same line with return.

EDIT: squid314's answer above is the right one

Community
  • 1
  • 1
dtheodor
  • 4,894
  • 3
  • 22
  • 27