まず第一に..長いコードで申し訳ありませんが、明確にしたい. (私の質問は一番下にあります)
1 人の顧客のすべての連絡先を取得しようとしています。まず、HTML コードでわかるように、getContacts(customer.id)
内部で関数を呼び出そうとしました。ng-repeat directive
しかし、これにより無限ループ ( $rootscope.infdig ) が発生しました。
だから私は関数をに移動しましたng-init directive
が、今はこのエラーが発生しています:
index.html
<tr ng-repeat="customer in customers">
<td>{{customer.name}}</td>
<td ng-init="contacts = getContacts(customer.id)">
<div ng-repeat="contact in contacts">
{{contact.firstname + " " + contact.lastname}}
</div>
</td
</tr>
CustomerService.js
これは、Mysql データベースから (PHP ファイル経由で) データを取得する JavaScript ファイルです。
angular.module("crmApp").factory("CustomerService", function ($http) {
return {
//get all customers
getCustomers: function () {
return $http.get("/php/customersGET").then(function (response) {
return response.data;
})
},
//get all contacts of 1 customer
getContactsByCustomersId: function (customers_id) {
return $http.get("/php/contactsGET/?customers_id=" + customers_id).then(function (response) {
return response.data;
});
}
});
CustomerController.js
関数の戻り値を console.log しようとしましたgetContactsByCustomersId()
が、次のように表示されます: d {$$state: Object}
angular.module("crmApp").controller("CustomerController", function ($scope, $http, CustomerService) {
//get all customers
CustomerService.getCustomers().then(function (data) {
$scope.customers = data;
});
//get all contacts by customers_id
$scope.getContacts = function (customers_id) {
return CustomerService.getContactsByCustomersId(customers_id).then(function (data) {
return data;
});
};
});
私の質問: '関数の返されたデータにアクセスgetContactsByCustomersId()
して、html コードで使用するにはどうすればよいですか???
何かが十分に明確でない場合は、私に尋ねてください! 前もって感謝します !!