Get parameter of the url via angularJS

1

Well, I need to get a parameter from my url as soon as my page loads.

localhost / 123456

The parameter would be: 123456

Using $ location, I got the url complete, but I can not get the parameter I need to load into my controller

 myApp.controller('Controller', function ($scope, traineeControlService, $location) {

var vm = this;
//getAll();

$scope.numberOfregistrationUser = {};

var searchObj = $location.url();

  console.log(searchObj);
}
    
asked by anonymous 06.02.2018 / 05:46

1 answer

2

One way would be to use ngRoute . Through $routeProvider you can configure your site's routes, and thereby extract the parameters from the URLs and inject them directly into your controller.

That is:

var myApp = angular.module('app', ['ngRoute']); // adicione o ngRoute como modulo do seu modulo

myApp.config(function($routeProvider) {

    // indica que quando tiver uma URL do tipo http://localhost/[id], que deve utilizar este template e controlador
    $routeProvider.when('/:id', {

        templateUrl: '[caminho para a sua pagina HTML]',
        controller: 'Controller' // nome do seu controlador
    }); 
})

myApp.controller('Controller', function ($scope, traineeControlService, $routeParams) {

    var vm = this;
    //getAll();
    $scope.numberOfregistrationUser = {};
    // var searchObj = $location.url();

    console.log($routeParams.id);
}

The $routeParams object contains all the parameters defined in the URL (note when the URL is configured, the use of : to indicate that it is a parameter that should be read).

    
06.02.2018 / 10:01