• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

[AngularJS] Nie działa ng-repeat

Object Storage Arubacloud
0 głosów
272 wizyt
pytanie zadane 16 stycznia 2017 w JavaScript przez Captivity Obywatel (1,030 p.)

Witam,

Przechodzę właśnie kurs AngularJS z eduweb. Do tej pory wszystko szło dobrze, a teraz mam problem, ponieważ nie działa mi pętla danych.
Mianowicie w kodzie mam:
 

<tr ng-repeat="client in clients">
                            <td class="text-center">{{ client.id }}</td>
                            <td>{{ client.company_name }}</td>
                            <td class="hidden-xs">{{ client.sector_name }}</td>
                            <td class="hidden-xs">{{ client.account_manager_name }}</td>
                            <td class="text-center"><a href="/clients/{{ client.id }}" class="label label-primary">Szczegóły</a></td>
                        </tr>

I mi się nic w tabeli nie wyświetla, za to jak użyję np. w pierwszym "td" samo {{ client }}, to dostaję cały obiekt:

[{"id":1,"company_name":"Lorem ipsum dolor","contact_name":"Michalina Kwiatkowska","contact_phone":"53 790 92 21","contact_email":"MichalinaKwiatkowska@dayrep.com","account_manager_id":1,"notes":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.","sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Konstantyn Kowalski"},{"id":2,"company_name":"Aenean massa","contact_name":"Kazimierz Wiśniewski","contact_phone":"88 434 60 33","contact_email":"KazimierzWiniewski@dayrep.com","account_manager_id":2,"notes":null,"sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Łukasz Pawłowski"},{"id":3,"company_name":"Nullam dictum","contact_name":"Miłosław Duda","contact_phone":"67 431 58 98","contact_email":"MiosawDuda@dayrep.com","account_manager_id":3,"notes":"Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.","sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Zofia Jabłońska"},{"id":4,"company_name":"Aliquam lorem ante","contact_name":"Konstancja Gorska","contact_phone":"78 931 86 71","contact_email":"KonstancjaGorska@dayrep.com","account_manager_id":4,"notes":"In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.","sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Izydor Pawłowski"},{"id":5,"company_name":"Maecenas tempus","contact_name":"Józef Woźniak","contact_phone":"79 319 44 99","contact_email":"JzefWoniak@teleworm.us","account_manager_id":5,"notes":null,"sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Sylwester Kozłowski"},{"id":6,"company_name":"Donec sodales sagittis","contact_name":"Janek Czerwinski","contact_phone":"66 796 79 49","contact_email":"JanekCzerwinski@jourrapide.com","account_manager_id":1,"notes":"Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu.","sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Konstantyn Kowalski"},{"id":7,"company_name":"Vestibulum purus","contact_name":"Hanna Kozłowska","contact_phone":"53 858 22 59","contact_email":"HannaKozowska@jourrapide.com","account_manager_id":2,"notes":null,"sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Łukasz Pawłowski"},{"id":8,"company_name":"Etiam sit","contact_name":"Walery Czarnecki","contact_phone":"67 677 77 97","contact_email":"WaleryCzarnecki@jourrapide.com","account_manager_id":3,"notes":"Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.","sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Zofia Jabłońska"},{"id":9,"company_name":"Cras id dui","contact_name":"Wacław Grabowski","contact_phone":"78 772 75 31","contact_email":"WacawGrabowski@rhyta.com","account_manager_id":4,"notes":"Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.","sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Izydor Pawłowski"},{"id":10,"company_name":"Duis arcu tortor","contact_name":"Augustyna Sokołowska","contact_phone":"69 268 36 68","contact_email":"AugustynaSokoowska@armyspy.com","account_manager_id":5,"notes":null,"sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Sylwester Kozłowski"},{"id":11,"company_name":"Phasellus accumsan","contact_name":"Jarek Kucharski","contact_phone":"78 357 89 49","contact_email":"JarekKucharski@armyspy.com","account_manager_id":1,"notes":"Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero.","sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Konstantyn Kowalski"},{"id":12,"company_name":"Pellentesque posuere","contact_name":"Felicja Olszewska","contact_phone":"88 706 67 24","contact_email":"FelicjaOlszewska@jourrapide.com","account_manager_id":2,"notes":null,"sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Łukasz Pawłowski"},{"id":13,"company_name":"Aenean viverra","contact_name":"Berta Ostrowska","contact_phone":"53 218 38 56","contact_email":"BertaOstrowska@teleworm.us","account_manager_id":3,"notes":null,"sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Zofia Jabłońska"},{"id":14,"company_name":"Phasellus gravida","contact_name":"Augustyn Nowicki","contact_phone":"79 435 93 31","contact_email":"AugustynNowicki@armyspy.com","account_manager_id":4,"notes":null,"sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Izydor Pawłowski"},{"id":15,"company_name":"Phasellus volutpat","contact_name":"Urszula Kucharska","contact_phone":"79 532 30 44","contact_email":"UrszulaKucharska@jourrapide.com","account_manager_id":5,"notes":"Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.","sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Sylwester Kozłowski"}]

Dlaczego nie mogę odwołać się do client.id, skoro dostając obiekt, to id jest?

3 odpowiedzi

+1 głos
odpowiedź 16 stycznia 2017 przez writen Nałogowiec (29,060 p.)
wybrane 16 stycznia 2017 przez Captivity
 
Najlepsza
To co jest na drugim listingu to tablica obiektów. Pokaż co zwraca sam serwis (co się znajduje w data).
komentarz 16 stycznia 2017 przez Captivity Obywatel (1,030 p.)

Dostaję obiekt, czyli zwrócone dane są chyba OK, tak jak na kursie:

Screen

1
komentarz 16 stycznia 2017 przez writen Nałogowiec (29,060 p.)

Proponuję również pracę z dokumentacją https://docs.angularjs.org/api/ng/service/$http

The response object has these properties:

data – {string|Object} – The response body transformed with the transform functions.

status – {number} – HTTP status code of the response.

headers – {function([headerName])} – Header getter function.

config – {Object} – The configuration object that was used to generate the request.

statusText – {string} – HTTP status text of the response.

Serwis $http w odpowiedzi zwraca obiekt zawierający te pięć właściwości wymienionych wyżej. Ty jednak wszystkie te rzeczy przekazujesz do widoku. Ciebie interesuje tylko właściwość data, więc to ją powinieneś przekazać do widoku. Chyba najlepiej zrobić to w serwisie, tak by zwracał tylko te dane, które chcesz.

...

function success(data){
    callback(data.data);
};
...

 

komentarz 16 stycznia 2017 przez Captivity Obywatel (1,030 p.)
Wow działa! Dziękuję bardzo za pomoc! :)
0 głosów
odpowiedź 16 stycznia 2017 przez ribeiro Stary wyjadacz (11,440 p.)

Tyle, że Twój kod działa. Być może nie otoczyłeś tr znacznikiem table?

 

<div ng-app>
  <div ng-controller="TodoCtrl">
    <table>
      <tr ng-repeat="client in clients">
           <td class="text-center">{{ client.id }}</td>
           <td>{{ client.company_name }}</td>
           <td class="hidden-xs">{{ client.sector_name }}</td>
           <td class="hidden-xs">{{ client.account_manager_name }}</td>
           <td class="text-center"><a href="/clients/{{ client.id }}" class="label label  primary">Szczegóły</a></td>
       </tr>
    </table>
  </div>
</div>

 

Oraz

 

function TodoCtrl($scope) {
  
  $scope.clients = [{"id":1,"company_name":"Lorem ipsum dolor","contact_name":"Michalina Kwiatkowska","contact_phone":"53 790 92 21","contact_email":"MichalinaKwiatkowska@dayrep.com","account_manager_id":1,"notes":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.","sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Konstantyn Kowalski"},{"id":2,"company_name":"Aenean massa","contact_name":"Kazimierz Wiśniewski","contact_phone":"88 434 60 33","contact_email":"KazimierzWiniewski@dayrep.com","account_manager_id":2,"notes":null,"sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Łukasz Pawłowski"},{"id":3,"company_name":"Nullam dictum","contact_name":"Miłosław Duda","contact_phone":"67 431 58 98","contact_email":"MiosawDuda@dayrep.com","account_manager_id":3,"notes":"Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.","sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Zofia Jabłońska"},{"id":4,"company_name":"Aliquam lorem ante","contact_name":"Konstancja Gorska","contact_phone":"78 931 86 71","contact_email":"KonstancjaGorska@dayrep.com","account_manager_id":4,"notes":"In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.","sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Izydor Pawłowski"},{"id":5,"company_name":"Maecenas tempus","contact_name":"Józef Woźniak","contact_phone":"79 319 44 99","contact_email":"JzefWoniak@teleworm.us","account_manager_id":5,"notes":null,"sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Sylwester Kozłowski"},{"id":6,"company_name":"Donec sodales sagittis","contact_name":"Janek Czerwinski","contact_phone":"66 796 79 49","contact_email":"JanekCzerwinski@jourrapide.com","account_manager_id":1,"notes":"Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu.","sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Konstantyn Kowalski"},{"id":7,"company_name":"Vestibulum purus","contact_name":"Hanna Kozłowska","contact_phone":"53 858 22 59","contact_email":"HannaKozowska@jourrapide.com","account_manager_id":2,"notes":null,"sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Łukasz Pawłowski"},{"id":8,"company_name":"Etiam sit","contact_name":"Walery Czarnecki","contact_phone":"67 677 77 97","contact_email":"WaleryCzarnecki@jourrapide.com","account_manager_id":3,"notes":"Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.","sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Zofia Jabłońska"},{"id":9,"company_name":"Cras id dui","contact_name":"Wacław Grabowski","contact_phone":"78 772 75 31","contact_email":"WacawGrabowski@rhyta.com","account_manager_id":4,"notes":"Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.","sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Izydor Pawłowski"},{"id":10,"company_name":"Duis arcu tortor","contact_name":"Augustyna Sokołowska","contact_phone":"69 268 36 68","contact_email":"AugustynaSokoowska@armyspy.com","account_manager_id":5,"notes":null,"sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Sylwester Kozłowski"},{"id":11,"company_name":"Phasellus accumsan","contact_name":"Jarek Kucharski","contact_phone":"78 357 89 49","contact_email":"JarekKucharski@armyspy.com","account_manager_id":1,"notes":"Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero.","sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Konstantyn Kowalski"},{"id":12,"company_name":"Pellentesque posuere","contact_name":"Felicja Olszewska","contact_phone":"88 706 67 24","contact_email":"FelicjaOlszewska@jourrapide.com","account_manager_id":2,"notes":null,"sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Łukasz Pawłowski"},{"id":13,"company_name":"Aenean viverra","contact_name":"Berta Ostrowska","contact_phone":"53 218 38 56","contact_email":"BertaOstrowska@teleworm.us","account_manager_id":3,"notes":null,"sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Zofia Jabłońska"},{"id":14,"company_name":"Phasellus gravida","contact_name":"Augustyn Nowicki","contact_phone":"79 435 93 31","contact_email":"AugustynNowicki@armyspy.com","account_manager_id":4,"notes":null,"sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Izydor Pawłowski"},{"id":15,"company_name":"Phasellus volutpat","contact_name":"Urszula Kucharska","contact_phone":"79 532 30 44","contact_email":"UrszulaKucharska@jourrapide.com","account_manager_id":5,"notes":"Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.","sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Sylwester Kozłowski"}];
}

komentarz 16 stycznia 2017 przez Captivity Obywatel (1,030 p.)

Naturalnie mam w table. Problem ewidentnie jest taki, że Angular nie widzi tego odniesienia po kropce.

W moim app.js mam:
 

app.controller('ClientsListCtrl', ['$scope', 'clients', function($scope, clients){
        
            $scope.clients = [];
            
            clients.getClients(function(clients){
                $scope.clients = clients;
                console.log($scope.clients);
            });
            
    }]);

A w services.js:
 

(function(){
    
    var app = angular.module('crmService', []);
    
    app.factory('clients', ['$http', function($http){
            
            var _getClients = function (callback){
                callback = callback||function(){};
                $http.get('/api.php/clients').then(success);
                        function success(data){
                            callback(data);
                        };
            };
            
            return {
                getClients: _getClients
            };
            
    }]);
    
})();

 

0 głosów
odpowiedź 16 stycznia 2017 przez Captivity Obywatel (1,030 p.)

Jak ręcznie wpisałem:
 

$scope.clients = [{"id":1,"company_name":"Lorem ipsum dolor","contact_name":"Michalina Kwiatkowska","contact_phone":"53 790 92 21","contact_email":"MichalinaKwiatkowska@dayrep.com","account_manager_id":1,"notes":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.","sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Konstantyn Kowalski"},{"id":2,"company_name":"Aenean massa","contact_name":"Kazimierz Wiśniewski","contact_phone":"88 434 60 33","contact_email":"KazimierzWiniewski@dayrep.com","account_manager_id":2,"notes":null,"sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Łukasz Pawłowski"},{"id":3,"company_name":"Nullam dictum","contact_name":"Miłosław Duda","contact_phone":"67 431 58 98","contact_email":"MiosawDuda@dayrep.com","account_manager_id":3,"notes":"Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.","sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Zofia Jabłońska"},{"id":4,"company_name":"Aliquam lorem ante","contact_name":"Konstancja Gorska","contact_phone":"78 931 86 71","contact_email":"KonstancjaGorska@dayrep.com","account_manager_id":4,"notes":"In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.","sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Izydor Pawłowski"},{"id":5,"company_name":"Maecenas tempus","contact_name":"Józef Woźniak","contact_phone":"79 319 44 99","contact_email":"JzefWoniak@teleworm.us","account_manager_id":5,"notes":null,"sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Sylwester Kozłowski"},{"id":6,"company_name":"Donec sodales sagittis","contact_name":"Janek Czerwinski","contact_phone":"66 796 79 49","contact_email":"JanekCzerwinski@jourrapide.com","account_manager_id":1,"notes":"Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu.","sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Konstantyn Kowalski"},{"id":7,"company_name":"Vestibulum purus","contact_name":"Hanna Kozłowska","contact_phone":"53 858 22 59","contact_email":"HannaKozowska@jourrapide.com","account_manager_id":2,"notes":null,"sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Łukasz Pawłowski"},{"id":8,"company_name":"Etiam sit","contact_name":"Walery Czarnecki","contact_phone":"67 677 77 97","contact_email":"WaleryCzarnecki@jourrapide.com","account_manager_id":3,"notes":"Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.","sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Zofia Jabłońska"},{"id":9,"company_name":"Cras id dui","contact_name":"Wacław Grabowski","contact_phone":"78 772 75 31","contact_email":"WacawGrabowski@rhyta.com","account_manager_id":4,"notes":"Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.","sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Izydor Pawłowski"},{"id":10,"company_name":"Duis arcu tortor","contact_name":"Augustyna Sokołowska","contact_phone":"69 268 36 68","contact_email":"AugustynaSokoowska@armyspy.com","account_manager_id":5,"notes":null,"sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Sylwester Kozłowski"},{"id":11,"company_name":"Phasellus accumsan","contact_name":"Jarek Kucharski","contact_phone":"78 357 89 49","contact_email":"JarekKucharski@armyspy.com","account_manager_id":1,"notes":"Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero.","sector_id":5,"sector_name":"Agencja reklamowa","account_manager_name":"Konstantyn Kowalski"},{"id":12,"company_name":"Pellentesque posuere","contact_name":"Felicja Olszewska","contact_phone":"88 706 67 24","contact_email":"FelicjaOlszewska@jourrapide.com","account_manager_id":2,"notes":null,"sector_id":3,"sector_name":"Producent odzieży","account_manager_name":"Łukasz Pawłowski"},{"id":13,"company_name":"Aenean viverra","contact_name":"Berta Ostrowska","contact_phone":"53 218 38 56","contact_email":"BertaOstrowska@teleworm.us","account_manager_id":3,"notes":null,"sector_id":1,"sector_name":"Sklep internetowy","account_manager_name":"Zofia Jabłońska"},{"id":14,"company_name":"Phasellus gravida","contact_name":"Augustyn Nowicki","contact_phone":"79 435 93 31","contact_email":"AugustynNowicki@armyspy.com","account_manager_id":4,"notes":null,"sector_id":2,"sector_name":"Wydawnictwo","account_manager_name":"Izydor Pawłowski"},{"id":15,"company_name":"Phasellus volutpat","contact_name":"Urszula Kucharska","contact_phone":"79 532 30 44","contact_email":"UrszulaKucharska@jourrapide.com","account_manager_id":5,"notes":"Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.","sector_id":4,"sector_name":"Producent suplementów","account_manager_name":"Sylwester Kozłowski"}];

To ładnie działa, czyli jednak jest coś nie tak z otrzymanymi danymi:
http://imgur.com/ykYaMhx

Podobne pytania

+1 głos
2 odpowiedzi 156 wizyt
pytanie zadane 3 kwietnia 2022 w JavaScript przez Bartek030 Obywatel (1,460 p.)
0 głosów
1 odpowiedź 111 wizyt
–1 głos
2 odpowiedzi 389 wizyt

92,666 zapytań

141,564 odpowiedzi

320,019 komentarzy

62,032 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...