在ng-repeat中使用模态窗口

时间:2014-04-11 18:56:03

标签: angularjs controller modal-dialog angularjs-ng-repeat angular-ui-bootstrap

我有一个ng-repeat,我正在尝试添加一个将同一范围变量传递给模态窗口的模态。我能够打开模态窗口但是ng-repeat的范围值没有显示在模态中。希望我的代码解释得更好。这就是我到目前为止所做的:

        <div ng-controller="CustomerController">  
        <div ng-repeat="customer in customers">
               <button class="btn btn-default" ng-click="open()">{{ customer.name }}</button>

                <!--MODAL WINDOW--> 
                <script type="text/ng-template" id="myModalContent.html">
                    <div class="modal-header">
                        <h3>The Customer Name is: {{ customer.name }}</h3>
                    </div>
                    <div class="modal-body">
                            This is where the Customer Details Goes<br />
                            {{ customer.details }}
                    </div>
                    <div class="modal-footer">

                    </div>
                </script>

        </div>  
        </div>

控制器:

   app.controller('CustomerController', function($scope, $timeout, $modal, $log, customerServices) {
    $scope.customers= customerServices.getCustomers();

    // MODAL WINDOW
    $scope.open = function () {

        var modalInstance = $modal.open({
          templateUrl: 'myModalContent.html',
             });

    };

});

以上打开模态窗口。但是,来自ng-repeat的客户详细信息(例如{{customer.name}})不会传递到模态窗口。控制器出了什么问题?

我在这里查看Angular Bootrap UI示例后尝试创建它:http://angular-ui.github.io/bootstrap/

修改

以下是jsfiddle示例: http://jsfiddle.net/Alien_time/8s9ss/3/

2 个答案:

答案 0 :(得分:12)

updated your fiddle以及代码也是如此。我希望这会有所帮助。

此致

var app = angular.module('app', ['ui.bootstrap']);

app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, customer)
{
$scope.customer = customer;

});

app.controller('CustomerController', function($scope, $timeout, $modal, $log) {

    $scope.customers = [
        {
        name: 'Ricky',
        details: 'Some Details for Ricky',
        },
        {
        name: 'Dicky',
        details: 'Some Dicky Details',
        },
        {
        name: 'Nicky',
        details: 'Some Nicky Details',
        }
    ];

    // MODAL WINDOW
    $scope.open = function (_customer) {

        var modalInstance = $modal.open({
          controller: "ModalInstanceCtrl",
          templateUrl: 'myModalContent.html',
            resolve: {
                customer: function()
                {
                    return _customer;
                }
            }
             });

    };

});

答案 1 :(得分:2)

这就是我设置我的模态以处理重复和想要编辑的项目的方法。我建议将其设置为使用不同的控制器,因为这样您就可以使用DI将已解析的项目注入子范围。

$scope.openModal = function(item) 
    // This sets up some of the options I want the modal to open with
    var options = {}
    angular.extend(options, {
      templateUrl: '/views/userItems/form.html',
      controller: 'ItemEditController',
      resolve: {
        // I resolve a copy of the so it dont mess up the original if they cancel
        item: function() { return angular.copy(item); }
      }
    });
    $modal.open(options).result.then(function(updatedItem) {
      // after the user saves the edits to the item it gets passed back in the then function
      if(updatedItem) {
        // this is a service i have to deal with talking to my db
        modelService.editItem(updatedItem).then(function(result) {
          // get the result back, error check then update the scope
          if(result.reason) {
            $scope.addAlert({type: 'error', title: 'Application Error', msg: result.reason});
          } else {
            angular.extend(vital, result);
            $scope.addAlert({type: 'success', msg: 'Successfully updated Item!'}); 
          }
        });
      }
    });
  };