Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable through URL with angular js

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;
    };

  }]);

})(); 
like image 670
Daniel Ortiz Avatar asked Sep 01 '14 16:09

Daniel Ortiz


People also ask

How do you pass a URL as a parameter in a URL in angular?

There are two primary ways to pass query parameters. The Angular router service comes bundled with the navigate method which lets you move from one route to another. The first argument to this method is an array of URL fragments that represents a route. This method also accepts a second argument in form of an object.


1 Answers

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;
    };

  }]);

})(); 
like image 62
ryeballar Avatar answered Sep 28 '22 16:09

ryeballar