Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Modal Window inside ng-repeat

I have a ng-repeat and I am trying to add a modal that passes the same scope variable to the modal window. I am able to get the modal window to open but the scope value from ng-repeat is not showing inside the modal. Hopefully my code explains better. This is what I have so far:

        <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>

The controller:

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

    };

});

The above opens the modal window. However, the customer details such as {{ customer.name }} from the ng-repeat is not passed into the modal window. Have I got something wrong with the Controller?

I am trying to create this after looking at the Angular Bootrap UI example here: http://angular-ui.github.io/bootstrap/

EDIT:

Here is a jsfiddle sample: http://jsfiddle.net/Alien_time/8s9ss/3/

like image 720
Neel Avatar asked Apr 11 '14 18:04

Neel


2 Answers

I've updated your fiddle and below code as well. I hope that will help.

Regards

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

    };

});
like image 92
sylwester Avatar answered Nov 15 '22 05:11

sylwester


This is how I setup my modals for handleing items that I ng-repeat over and want to edit. I suggest setting it up to work with a different controller, because then you can use DI to inject the resolved item to the child scope.

$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!'}); 
          }
        });
      }
    });
  };
like image 36
Mindstormy Avatar answered Nov 15 '22 07:11

Mindstormy