我有这个控制器:
app.controller('BuscadorClientesController', function(){
this.clientes = [
{
tipo: {
nombre: "Sarasa"
},
nombre: "Alan",
direccion: "Fake Address 1234",
telefono: "12341234",
email: "ipsum@gmail.com",
rubro: "Lorem",
id: "1"
}
]
});
在我看来,“客户”数组打印得很好,但现在我想从我的数据库中获取我的客户,所以我做了这个
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) 正在打印正确的数据(一个包含很多对象的数组),但它没有显示在我的视图中:
<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>
我究竟做错了什么?
编辑:
我将控制器代码更改为:
app.controller('BuscadorClientesController', function(Clientes){
var that = this
Clientes.getAll(function(data){
that.clientes = data
console.log($scope.clientes)
});
});
这是正确的方法还是有更好的方法?