dongnao9525 2014-03-10 15:09
浏览 34
已采纳

Ajax请求列表中的特定项

I'm new to AngularJS and I don't know how to accomplish a task. In my system I have a database with two tables: "Customers" and "Offices". This is a 1:N relation. In my HTML page I have a list (actually is a table) that contains info about the customers (info extracted from the "Customers" table). For each row of the table, you can find a button. If you click on that button some infos about the offices of THAT customer are loaded from the database.

The first part is quite simple. This is the Angular service that loads customers info:

angular.module("app").factory("Customer", ["$resource", 
    function($resource){
        return $resource("/customers/list", 
                            {format:'json'},
                            {
                                find: {
                                       method:'GET'
                                }
                            });
}]);

This is the Angular controller that exposes an array of customers:

angular.module("app").controller("customersController", ["Customer", "$scope",
    function (Customer, $scope){
        var customers = new Array();
        var metacustomers = Customer.find(function(){
            for(var i=0; i<metacustomers.results.length; i++){
                customers[i] = new Object();
                customers[i].customerName = metacustomers.results[i].customer_name;
                customers[i].customerID = metacustomers.results[i].customer_ID;
            }
            $scope.customers = customers;
        });
}]);

And this is the part of the view that shows info:

<table>
                    <thead>
                            <th></th>
                            <th></th>
                            <th></th>
                    </thead>
                    <tr data-ng-repeat="customer in customers | filter:query">
                            <td>
                                    <a href="#expan-offices">+</a>
                            </td>
                            <td>
                                <strong>{{customer.customerName}}</strong>
                            </td>
                            <td>
                                <div>           
                                    <a title="Modify customer" href="/customers/post/{{customer.customerID}}">
                                    </a>
                                    <a title="Delete customer" href="/customers/delete/{{customer.customerID}}">
                                    </a>
                                </div>
                            </td>
                        </tr>
                </table>

All of this looks like:

customers list

The problem arrives with the second task. When I click on the "+" button I must load the offices infos about THAT customer (the customer on the same row of the button). I need the customerID to retrieve these infos and I have it. But, how can I say to Angular to load exactly the info belongs to the right customer? I don't know how to model this situation. I need your help. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • duanan1228 2014-03-10 15:23
    关注

    You can pass the customer to the ng-click directive like so:

     <a href="#expand-offices" ng-click="expand(customer)">+</a>
    

    And then create an expand function in your controller which takes a customer as its input. Something like this:

    $scope.expand = function(customer){
        //fetch the customer
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目