0

まず第一に..長いコードで申し訳ありませんが、明確にしたい. (私の質問は一番下にあります)

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 コードで使用するにはどうすればよいですか???

何かが十分に明確でない場合は、私に尋ねてください! 前もって感謝します !!

4

1 に答える 1

0

ある種の SQL 結合を使用して、顧客と連絡先を返す別の php アクションを追加する必要があります。

次に、ネストされた ng-repeat を次のように使用できます。

<tr ng-repeat="customer in customers">
    <td>{{customer.name}}</td>
    <td>
        <div ng-repeat="contact in customer.contacts">
            {{contact.firstname + " " + contact.lastname}}
        </div>
    </td
</tr>
于 2015-09-03T09:59:13.643 に答える