Why isn’t $routeParams bringing the id?

Asked

Viewed 36 times

0

I’m using $routeParams.idunidade to bring from another screen the id of a unit but the idunidade is always "Undefined" and I can’t understand why, because I’ve used it and it always worked! Will the way of use changed?

Follows my codes:

<div class="row corpo">
    <div class="col-ms-12 exibeUnidades">
        <div>
            <table>
                <tr>
                    <td width="200"><b>Unidade</b></td>
                    <td width="200"><b>Endereço</b></td>
                    <td width="200"><b>Bairro</b></td>
                    <td width="100"><b>Cidade</b></td>
                    <td></td>
                </tr>
                <tr ng-repeat="u in unidades">
                    <td>{{u.unidade}}</td>
                    <td>{{u.endereco}}, {{u.numero}}</td>
                    <td>{{u.bairro}}</td>
                    <td>{{u.cidade}}</td>
                    <td><a href="#/unidade-editar/{{u.idunidade}}">editar</a></td>
                </tr>
            </table>
        </div>
    </div>
</div>

Controller:

app.controller("UnidadeEditarCtrl", ['$scope', '$http', '$window', '$location', '$rootScope', '$routeParams', function ($scope, $http, $location, $rootScope, $routeParams) {

$scope.idempresa = localStorage.getItem('domanda_idempresa');
$scope.empresa = localStorage.getItem('domanda_empresa');


var getUnit = function(){
    var idunidade = $routeParams.idunidade;
    console.log('idunidade ' + idunidade)
}
getUnit();

print do console.log

1 answer

0

I believe it is because Voce has injected one less dependency into the function. So what Voce thinks is the $routeParams in fact is the $rootScope.

I recommend you take a look at styleguide of the Angular.js made by the Pope Jhon.

  • Hi Leonardo. No, I did not make this change or confusion. But it worked again, I gave a "command + R" and it worked. Thanks

  • Still it is wrong the snippet that you put here (may have been typo or something). But the string array for injection has these arguments in the following order ['$scope', '$http', '$window', '$location', '$rootScope', '$routeParams'] whereas your controller’s function has the following function ($scope, $http, $location, $rootScope, $routeParams). Saw that the $window in his role?

  • Does it make a difference? The order and lack of $window?

  • 1

    Yes, the order and quantity of items that Voce reports in the Array should be the same in its function. Angularjs uses this array of strings to search within it where these dependencies are. After it finds them it takes its function (controller) and calls it passing all the dependencies in the order it was informed. This is why it is important to keep the order Voce reports in the dependency array and the arguments of its function/controller.

  • Thanks for the information Leonardo!

Browser other questions tagged

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