Skip to content Skip to sidebar Skip to footer

Pass A Variable Into $routeProvider In Angular Between Views? - Angularjs

Wondering if there's an 'angular specific' way to try and achieve this. I have a page with some views. When a user clicks an anchor, the views change, simple enough. What I'm curi

Solution 1:

You need to add the parameter to your $routeProvider template:

sampleApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/' + nameHolder, {
        templateUrl: 'templates/individkcd/:nameHolder',
        controller: 'individ'
    }).
      when('/', {
        templateUrl: 'templates/home.html'
    });
}]);

Then in your target controller use $routeParams:

sampleApp.controller('individ', function($scope, $routeParams) {

    $scope.img =  $routeParams.nameHolder;


});

From the $routeProvider docs linked above:

path can contain named groups starting with a colon: e.g. :name. All characters up to the next slash are matched and stored in $routeParams under the given name when the route matches


Post a Comment for "Pass A Variable Into $routeProvider In Angular Between Views? - Angularjs"