1

I have a problem pushing items inside the array of a modal. I'm getting undefine value.

I followed all the examples that i can find but it is not working when it's done on model.

Here is the plunk http://plnkr.co/edit/xMiwyw?p=preview

My Code:

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

    myApp.controller('ordersCtrl', 
        function ordersCtrl($scope, $modal){

            $scope.open = function () {

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

             };

    });

var ModalInstanceCtrl = function ($scope, $modalInstance, $http) {

    $scope.dealerCount = [];

    // Add a Item to the list
    $scope.addItem = function () {
        $scope.dealerName = dealerName;

        $scope.dealerCount.push({
            name: $scope.dealerName
        });

        // Clear input fields after push
        $scope.dealerName = "";

    };

      $scope.ok = function () {
        $modalInstance.close($scope.selected.item);
      };

      $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
      };
    };

Modal Template:

<div class="row" id="AddItem">
   <div class="col-md-6"><p class="input-group">
    <input value="" type="text" placeholder="Name of Item" ng-model="dealerName"><button ng-click="addItem()">Add to list</button></p>
   </div>
</div>
<div class="row">
   <tr ng-repeat="item in dealerCount" class="item-unchecked">
   <td><b>Dealer:</b> {{item.name}}</td>
   </tr>
</div>

1 Answer 1

0

works here

You have to share data between controllers in some way. The one way of doing that is to create a service

myApp.factory('data', function(){
  return {
    dealerCount : [],
    dealerName: ''
  }
});

You alse may see this video for better understanding

1
  • It worked. I had been cracking my head the whole day figuring out how to make it work. The video helped a lot in better understanding the concept. Thank you
    – MarkJ
    Feb 20, 2014 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.