Suny Saju Suny Saju - 4 months ago 8
AngularJS Question

how to write different post method in angularjs?

i' using AngularJS v1.4.2. i have 2 html page , they have 2 controller.both controller have save event. how to use use http post method

first controller i'm calling post method given below


var promisePost = crudService.post(Countries);

promisePost.then(function (pl) {


alert("Sucessfully Inserted")
getCountry();
$stateParams.country = "";
}, function (err) {
alert("NOt Inserted")

});


second controller i'm calling post method given below


var promisePost = crudService.post(Levels);


promisePost.then(function (pl) {
alert("Sucessfully Inserted")
getLevel();

}, function (err) {
alert("NOt Inserted")

});


my app.js


myapp.service('crudService', function ($http, RESOURCES) {


//Create new record

this.post = function (Country) {
var request = $http({
method: "post",
url: RESOURCES.baseUrl + "saveCountry",
data: Country
});
return request;
}
this.post = function (Level) {
var request = $http({
method: "post",
url: RESOURCES.baseUrl + "saveLevel",
data: Level
});
return request;
}


});

but this code only take last post method.How to selecet post method properly. Anyone can helpme?

Answer

User countryPost and levelPost as follows and call those accordingly.

myapp.service('crudService', function ($http, RESOURCES) {

//Create new record

this.countryPost= function (Country) {
    var request = $http({
        method: "post",
        url: RESOURCES.baseUrl + "saveCountry",
        data: Country
    });
    return request;
}
this.levelPost= function (Level) {
    var request = $http({
        method: "post",
        url: RESOURCES.baseUrl + "saveLevel",
        data: Level
    });
    return request;
}
});
Comments