0

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' });
}]);
1
  • 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' }); }]); Commented Jan 4, 2017 at 13:43

1 Answer 1

3

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;
   });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.