2

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?

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

4 Comments

I changed my code to yours but its still not working :(
@AlanM try it with <td>{{ cliente.tipo.nombre}}</td> and let me know if that works and i'll update my answer
It wont, I have changed $interpolateProvider.startSymbol and endSymbol to "<%=", "%>" because I am using Laravel and its template engine also uses "{{" and "}}"
I found a solution, check it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.