2

私はこのコントローラーを持っています:

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"
        }
    ]
});

私の見解では、「clientes」配列は正常に印刷されていますが、データベースからクライアントを取得したいので、これを作成しました

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)
    });
});

それは正しい方法ですか、それとももっと良い方法はありますか?

4

2 に答える 2

0

変数を表示する方法が正しくありません。AngularJS では、ngBindまたはexpressionを使用する必要があります。

ビューは次のようになります。

<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>
于 2015-04-19T00:40:38.577 に答える