How to get parameters URL AngularJS

0

How to get URL parameter with AngularJS. If anyone has an example I would appreciate it.

$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

    
asked by anonymous 18.07.2018 / 02:57

1 answer

0

using $stateProvider , would look like this:

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

and your controller:

.controller('MyCtrl', ['$scope','$routeParams', function($scope,$stateParams, $state) {
    var param1 = $state.param1;
    var param1 = $state.param2;
}]);
    
18.07.2018 / 05:22