How to get Angularjs URL parameters

Asked

Viewed 500 times

0

How to get parameter of the URL with Angularjs. If anyone has any example I thank.

$scope.pegaCNPJ = function () {
        var cnpjs = $("#prospectCNPJ").val().replace(/[^\d]+/g, '');
        $.post('models/verificaCliente.php', {prospectCNPJ: cnpjs}, function (retorno) {
            console.log(retorno);
            $scope.dados = retorno;
            if (retorno.length > 0) {
                $('#processando').show();
                $timeout(function () {
                    $state.go('cadOportunidade', {id: retorno});
                }, 3000);
            } else {
                $http.jsonp("https://www.receitaws.com.br/v1/cnpj/" + cnpjs, {jsonpCallbackParam: 'callback'}).then(function (response) {
                    $scope.valor = response;
                });
            }
        });
    };

angular.module('App').config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
    $locationProvider.hashPrefix(''); // by default '!'
    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'views/dashboardAdmin.php',
            controller: 'AppCtrl',
            resolve: {
                'title': ['$rootScope', function ($rootScope) {
                    $rootScope.title = 'Home';
                }],
            }
        })
        .state('dashboardAdmin', {
            url: '/dashboardAdmin',
            templateUrl: 'views/dashboardAdmin.php',
            controller: 'AppCtrl',
            resolve: {
                'title': ['$rootScope', function ($rootScope) {
                    $rootScope.title = 'Dashboard';
                }],
            }
        })
        .state('cadProspect', {
            url: '/cadProspect',
            templateUrl: 'views/cadProspect.php',
            controller: 'AppCtrl',
            resolve: {
                'title': ['$rootScope', function ($rootScope) {
                    $rootScope.title = 'Cadastro Prospect';
                }],
            }
        })

        .state('cadOportunidade', {
            url: '/cadOportunidade/:id',
            templateUrl: 'views/cadOportunidade.php',
            controller: 'AppCtrl',
            resolve: {
                'title': ['$rootScope', function ($rootScope) {
                    $rootScope.title = 'Cadastro Oportunidade';
                }],
            }
        })
});

]1]1

  • See if it helps: https://answall.com/questions/274553/geteven%C3%A2metro-da-url-via-Angularjs

1 answer

0


using $stateProvider, would look like this:

$stateProvider.state('/viewPage/:param1/:param2', {
    templateUrl: 'partials/partial1.html',
    controller: 'MyCtrl'
    ...
});

and its controller:

.controller('MyCtrl', ['$scope','$routeParams', function($scope,$stateParams, $state) {
    var param1 = $state.param1;
    var param1 = $state.param2;
}]);
  • @Herbert de Lima, I’ve managed to move the parameters to another page. I am passing the ID and client name, but the url is appearing like this http://localhost/Prospect/app/#/cadOportunidade/1/GF%20VIEIRA%20E%20CIA%20LTDA, after the ID appears the company name in the url, you know how I can fix it.

  • just mount the way you want it to get the url: dominio.com.br/usuairo/detalhes or dominio.com.br/usuario?filtros=idade,sobrenome would be .state(/usuario?filtros.:*) you accessing by $state.filtros sera an array with all parameters, I have not tested here, but if I am not mistaken it is so

Browser other questions tagged

You are not signed in. Login or sign up in order to post.