angularjs - Webservice Call using Angular in MVC -
can 1 please explain how can call action using angular in mvc project?
i managed call action using ajax this:
var app = angular.module('toprightsec', ['ng']); app.controller('nationalityctrl', ['$scope', function ($scope, $http) { $scope.items = []; var items = populatelistfromlocalstorage("offices", "login/getoffices", 24); var officelist = ""; (var = 0; < items.length; i++) { $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') }; } $scope.reloadpage = function () { window.location.reload(); } $scope.getresult = function ($index, item) { $.ajax({ type: 'get', async: true, url: 'login/changeoffice', contenttype: "application/json; charset=utf-8", datatype: "json", data: { officeid: $scope.items[$index].guid, officename: $scope.items[$index].name, }, success: function (msg) { } }); }; }]);
i tried changing angular this:
var angularmodule = angular.module('toprightsec', ['ng']); angularmodule.service('apicall', ['http', function ($http) { var result; this.postapicall = function (controllername, methodname, obj) { debugger; result = $http.post('api/' + controllername + '/' + methodname,obj).success(function (data, success) { result = (data); }).error(function () { ("something went wrong"); }); return result; }; }]); angularmodule.controller('nationalityctrl', ['$scope', function ($scope, $http, apicall) { $scope.items = []; var items = populatelistfromlocalstorage("offices", "login/getoffices", 24); var officelist = ""; (var = 0; < items.length; i++) { $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') }; } $scope.reloadpage = function () { window.location.reload(); } $scope.getresult = function ($index, item) { var obj = { 'officeid' : '123', 'officename' : 'sample' } var result = apicall.postapicall("login", "changeoffice", obj).success(function (data) { var data = $.parsejson(json.parse(data)); $scope.message = data; }); }; }]);
i keep getting error "postapicall" not defined on browser console.
any idea doing wrong here?
thanks.
user promise, return when $http done:
this.postapicall = function (controllername, methodname, obj) { debugger; var deferred = $q.defer(); $http.post('api/' + controllername + '/' + methodname,obj).success(function (data) { deferred.resolve(data); }); return deferred.promise; }; var result = apicall.postapicall("login", "changeoffice", obj).then(function (data) { var data = $.parsejson(json.parse(data)); $scope.message = data; });
Comments
Post a Comment