Creating an angular controller

-2

I'm setting a controller where one of the features is to get a data in the bank through its id. However, nothing appears on the console when I put this

Code that shows the countries on the screen

<table width="200">
    <tr>
        <td><b>País</b></td>
        <td><b>Sala</b></td>
    </tr>
    <tr ng-repeat="pais in paises">
        <td>{{pais.nome}}</td>
        <td>{{pais.sala}}</td>
        <td><a href="#/editarPais/{{pais.idPais}}">editar</a></td>
    </tr>
</table>

html code

<form>
    <input type="hidden" ng-model="pais.idPais">
    País <input type="text" ng-model="pais.nome">
    Sala <input type="text" ng-model="pais.sala">
    <button ng-click="atualizarPais(pais)">Atualizar</button><br>
</form>

Agile code

app.controller("PaisesController", function ($scope, $http, $routeParams, $state) {

var carregaPaises = function () {
    $http.get("admin/php/pegaPaises.php").success(function (data){
        //console.log(data);
        $scope.paises = data;
    });
};

$scope.adicionaPais = function (pais) {
    $http.post("admin/php/adicionaPais.php", pais).then(function (data){
        //console.log(data);
        carregaPaises();
    })
};

var carregaPais = function (pais) {
    console.log($routeParams.pais);
};

carregaPaises();

carregaPais();

});

Config code:

var app = angular.module("vc", ["ui.router", "ngRoute"]);
.state("paises", {
        url: "/paises",
        controller: "PaisesController",
        templateUrl: "admin/views/paises.html"
    })

    .state("editarPais", {
        url: "/editarPais/:idPais",
        controller: "editarPaisController",
        templateUrl: "admin/views/editarPais.html"
    })

How do I see the data that comes in the parameter?

    
asked by anonymous 11.01.2016 / 19:45

1 answer

1

Your problem may be due to an injection failure of the parameter. Try to use $routeParams in the injection of controller , like this:

app.controller("editarPaisController", function ($scope, $http, $routeParams) {

But I do not know how you're doing the parameter definition in the url, as this is not the Angular method of setting a parameter. Since you said that you are using ui-router , I recommend that you do the correct way, including the access link, where it should be done as follows:

html

<td><a ui-sref="meuestado({idPais: pais.idPais})">editar</a></td>

controller

app.controller("editarPaisController", function ($scope, $http, $state) {
    var urlParam = $state.params.idPais;
});

State

.state('meuestado', {
    url: 'editarPais/:idPais',
    //..outras opções aqui
})
    
11.01.2016 / 20:17