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?