Alan
Alan

Reputation: 2639

Retrieving and showing data from API with AngularJS $http

I have this controller:

app.controller('BuscadorClientesController', function(){
    this.clientes = [
        {
            tipo: {
                nombre: "Sarasa"
            },
            nombre: "Alan",
            direccion: "Fake Address 1234",
            telefono: "12341234",
            email: "[email protected]",
            rubro: "Lorem",
            id: "1"
        }
    ]
});

In my view the "clientes" array is being printed fine but now I want to get my Clients from my database so I made this

app.service('Clientes', function ($http) {
    this.getAll = function (success, failure) {
        $http.get('/api/clientes')
            .success(success)
            .error(failure);
    }
});

app.controller('BuscadorClientesController', function($scope, Clientes){
    Clientes.getAll(function(data){
        $scope.clientes = data
        console.log($scope.clientes)
    });
});

console.log($scope.clientes) is printing the right data (an array with a lot of objects) but its not being displayed in my view:

<tr ng-repeat="cliente in buscador.clientes">
    <td><%= cliente.tipo.nombre %></td>
    <td><%= cliente.nombre %></td>
    <td><%= cliente.direccion  %></td>
    <td><%= cliente.telefono  %></td>
    <td><%= cliente.email  %></td>
    <td><%= cliente.rubro  %></td>
</tr>

What am I doing wrong?

EDIT:

I changed the controller code to this:

app.controller('BuscadorClientesController', function(Clientes){
    var that = this
    Clientes.getAll(function(data){
        that.clientes = data
        console.log($scope.clientes)
    });
});

Is it the correct way to do it or is there any better way?

Upvotes: 2

Views: 1265

Answers (2)

dting
dting

Reputation: 39287

If you are using the controller as syntax then your edited code is correct.

controller:

app.controller('BuscadorClientesController', function(Clientes){
    var vm = this;
    Clientes.getAll(function(data){
        vm.clientes = data;
        console.log(vm.clientes);
    });
});

html:

<tr ng-repeat="cliente in buscador.clientes">
    <td><%= cliente.tipo.nombre %></td>
    <td><%= cliente.nombre %></td>
    <td><%= cliente.direccion  %></td>
    <td><%= cliente.telefono  %></td>
    <td><%= cliente.email  %></td>
    <td><%= cliente.rubro  %></td>
</tr>

If you are not, then the clientes is on your scope and should not be prefixed by buscador:

controller:

app.controller('BuscadorClientesController', function($scope, Clientes){
    Clientes.getAll(function(data){
        $scope.clientes = data
        console.log($scope.clientes)
    });
});

html:

<tr ng-repeat="cliente in clientes">
    <td><%= cliente.tipo.nombre %></td>
    <td><%= cliente.nombre %></td>
    <td><%= cliente.direccion  %></td>
    <td><%= cliente.telefono  %></td>
    <td><%= cliente.email  %></td>
    <td><%= cliente.rubro  %></td>
</tr>

Upvotes: 2

user9903
user9903

Reputation:

You're using an incorrect way to display variables. In AngularJS you have to use ngBind or expressions.

Your view should look like this:

<tr ng-repeat="cliente in buscador.clientes">
  <td ng-bind="cliente.tipo.nombre"></td>
  <td ng-bind="cliente.nombre"></td>
  <td ng-bind="cliente.direccion"></td>
  <td ng-bind="cliente.telefono"></td>
  <td ng-bind="cliente.email"></td>
  <td ng-bind="cliente.rubro"></td>
</tr>

Upvotes: 0

Related Questions