Passing variable through URL with angular js

All we need is an easy explanation of the problem, so here it is.

I am using angular to make an e-commerce, and I’m setting an infinite scroll to the products list page. Everything worked fine, but I want to use the URL to set the page, so the user can access an specific page through URL. how do I set a variable like “pageNumber” in the URL with angular? like “www.page.com/page/2/”(I want to get the number 2 and pass it to the store controller)

Here’s the code I have now

(function() {
var app = angular.module('concurseirosUnidos', ['store-directives', 'ngRoute']);
    app.config(function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(true);
    $routeProvider
    .when('/', {templateUrl: 'partials/products-list.html'})
    .when("/page/$pageNumber"), {
        // probably I'd need to put something here?
    })
     .otherwise({redirectTo:'/'});;
    }
});

  app.controller('StoreController', ['$http', '$scope', function($http, $scope){
    var store = this;
    store.products = [];      

    $http.get('/app/products/products.json').success(function(data){
        store.products = data;
    });

    if(typeof page === 'undefined'){
        var page = 1;   
    }else{
      //if it's defined through the url, page = pageNumberFromURL
    }
    $scope.myLimit = 3 * page;

    $scope.nextPage = function () {
        page++; // I want this function to actually update the url and get the variable from there
        $scope.myLimit = 3 * page;
    };

  }]);

})(); 

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You use $routeParams to get the values of a specific named group in a $route definition.

REFERENCE

Example:

.config(function($routeProvider) {
  $routeProvider.when('/page/:page_number', {
    // your route details
  });
})

.controller('Ctrl', function($scope, $routeParams) {
  console.log($routeParams.page_number); // prints the page number
});

In relation to your code, it should look something like this:

(function() {
var app = angular.module('concurseirosUnidos', ['store-directives', 'ngRoute']);
    app.config(function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(true);
    $routeProvider
    .when('/', {templateUrl: 'partials/products-list.html'})
    .when("/page/:page_number"), {
        templateUrl: 'partials/page.html', // I made this up
        controller: 'StoreController'
    })
     .otherwise({redirectTo:'/'});;
    }
});

  app.controller('StoreController', ['$http', '$scope', '$routeParams', function($http, $scope, $routeParams){
    var store = this;
    var page = $routeParams.page_number;
    store.products = [];      

    $http.get('/app/products/products.json').success(function(data){
        store.products = data;
    });

    if(typeof page === 'undefined'){
        var page = 1;   
    }else{
      // if $routeParams.page_number is defined to you implementation here!
    }

    $scope.myLimit = 3 * page;

    $scope.nextPage = function () {
        page++; // I want this function to actually update the url and get the variable from there
        $scope.myLimit = 3 * page;
    };

  }]);

})(); 

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply