calling webapi from angularjs service - Unable to get property 'post' of undefined or null reference -
i have following function in service:
myapp.service('addparentservice', function ($http) { vm.returnparent = function ($http) { (i in vm.parent) { //alert('getvalues : ' + vm.parent[i].name); } var data = { name: 'jeff', email: 'jv@test.com', phone: '5551212', carriername: 'att' }; $http.post( 'http://localhost:10000/api/people/postregister/', json.stringify(data), { headers: { 'content-type': 'application/json' } } ).success(function (data) { alert(result); }); }); i ran issue wasn't injecting $http. when run function following error:
unable property 'post' of undefined or null reference
this controller:
myapp.controller('addparentcontroller', function ($scope,$http, addparentservice) { $scope.addparentservice = addparentservice; });
you need inject $http in service, if haven't already. no need inject on controller if you're not invoking there (which guess you're not doing, because if are, what's point in making new service...?)
however, you're including $http (which service) parameter of returnparent function; that's why undefined or null reference error pops up.
so, first line of code should vm.returnparent = function () {
good luck!
Comments
Post a Comment