0

首先..对不起长代码,但我想澄清一些。(我的问题位于底部)

我正在尝试获取 1 个客户的所有联系人,首先我尝试在 HTML 代码中getContacts(customer.id)调用该函数。ng-repeat directive但这导致了一个 inifine 循环($rootscope.infdig)。

所以我把函数移到了,ng-init directive但现在我收到了这个错误:

意外的令牌 <

索引.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>

客户服务.js

这是一个 javascript 文件,我从 Mysql 数据库中获取一些数据(通过 PHP 文件)

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

客户控制器.js

我试图 console.loggetContactsByCustomersId()函数的返回值,但它显示如下: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

您应该添加另一个 php 操作,该操作使用某种 sql 连接返回客户以及联系人。

然后你可以像这样使用嵌套的 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 回答