Sumit Chikane
Sumit Chikane

Reputation: 139

How to add get request parameter in Angularjs

How to add get request parameter from url to controller in angularjs e.g. my request is http://localhost/abc-ang/#!/abc/8 and my controller code is

app.controller('AbcCtrl', function($scope,$http) {
   $http.get("src/public/add/:id").then(function(response) {
       $scope.abc = response.data;
   });
});

I want to replace src/public/add/:id to src/public/add/8 how can I do that dynamically?

My routing configuration is

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/abc', {
            templateUrl: "tpl/welcome.html"
        })
        .when('/abc/:wineId', {
            templateUrl: 'tpl/abc-details.html', 
            controller: 'AbcDetailCtrl'
        })
        .otherwise({ redirectTo: '/abc' });
}]);

Upvotes: 0

Views: 52

Answers (1)

Mistalis
Mistalis

Reputation: 18279

You can access URL params in your code with $routeParams:

From your comment your route is:

$routeProvider.when('/abc/:wineId', {
    templateUrl: 'tpl/abc-details.html',
    controller: 'AbcDetailCtrl'
});

So in your controller, you can get wineId value with:

app.controller('AbcCtrl', function($scope, $http, $routeParams) {
   $http.get("src/public/add/" + $routeParams.wineId).then(function (response) {
       $scope.abc = response.data;
   });
});

Upvotes: 3

Related Questions