Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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' });
    }]);
share|improve this question
1  
coudl you please add your routing configuration – Manuel Obregozo 10 mins ago
    
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' }); }]); – Sumit Chikane 8 mins ago

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;
   });
});
share
    
that's correct, update the answer according to their id, which is wineId – Manuel Obregozo 6 mins ago
    
Done while you were writting your comment! ;P – Mistalis 6 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.