Arav Arav - 1 year ago 101
AngularJS Question

angular route provider - post request

How can I do a post request to a url using routeprovider? Provided sample code below

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
.when("/", {
templateUrl : "main.htm"
.when("/red", {
templateUrl : "red.htm"
.when("/green", {
templateUrl : "green.htm"
.when("/blue", {
templateUrl : "blue.htm"

Answer Source

You can use a resolve:

.when("/", {
    templateUrl : "main.htm",
    resolve: {
      data: function($http) {
        $'/yourUrl', yourData)
            .then(function(res) {
              return res;
            }, function(err) {
              return null;

And then in your controller,

.controller(function(data) {

NOTE: This is not using routeProvider per se, because making REST calls is not what the routeProvider is for. Angular can do that only through the $http service. I am assuming that you just want to make a REST call from within your route definition.

Protip A better way of doing this would be to define a service in your module, and insert the module in the route resolve, rather than injecting $http directly. I've done that here only for brevity

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download