yeddez
yeddez

Reputation: 365

I lose my $scope value

I'm still new with angularJs I'm trying to pass a value of a variable from a page html to another page(actually a modal).Both of the pages share the same controller . Would help me to diagnose the pb please

Html Page1:

    <button type="submit" class="btn btn-primary pull-right"  
                        data-ng-click="ctrl.nextOperation(chosenProducts,'lg')">Next</button>
                </div>  

app.js :

self.nextOperation = function(chosenProducts,size)
{
    $scope.chosenProducts= chosenProducts;
    $scope.product = chosenProducts[0];
    console.log($scope.product.nameProduct);
    console.log("test test nextt Operation "+chosenProducts[0].nameProduct);       
    var modalInstance = $uibModal.open({
        animation : $scope.animationsEnabled,
        templateUrl : 'partials/operation.html',
        controller : 'ProductsController',
        scope : $scope,
        size : size,
        resolve : {
                        }
    });

};

Page 2 :

<table>
            <tbody>

              <tr class="cake-bottom" >
                <td class="cakes">                
                    <div class="product-img2">
                    </div>
                </td>
                <td class="cake-text">
                    <div class="product-text">
                        <h3>{{product.nameProduct}}</h3>
                        <p>Product Code: {{product.numSerie}}</p>
                    </div>
                </td>
                <td class="quantity">                
                  <div class="product-right">
                     <input min="1" type="number" id="quantity" name="quantity" value="" class="form-control input-small">                
                  </div>
                </td>
                <td>
                    <h4>{{product.price}}</h4>
                </td>
                <td class="btm-remove">
                 <div class="close-btm">
                   <h5>Remove</h5>
                </div>
                </td>


                 </tr>
           </tbody>
        </table>

Upvotes: 2

Views: 154

Answers (3)

yeddez
yeddez

Reputation: 365

Thanks for your help! I could solve the problem, I had to not define the controller in my modal

Upvotes: 0

Gene
Gene

Reputation: 616

Here is a full example of a Bootstrap modal and passing variables from the parent to the modal controller. Plunker Here

index.html

<!DOCTYPE html>
<html>

  <head>
    <link data-require="[email protected]" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
    <script data-require="[email protected]" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
    <script data-require="[email protected]" data-semver="1.1.2" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.1.2/ui-bootstrap-tpls.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="my-app">
    <div ng-controller="myController as mc">
      <h1>Hello Plunker!</h1>
      <br><br>
      myObject = {{mc.myObject}}
      <a ng-click="mc.openModal(mc.myObject)">Open Modal</a>
    </div>
  </body>

</html>

myModalContent.html

<h2>I am a modal</h2>
{{modal.myObject}}

script.js

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

myApp.controller('myController', function($uibModal) {
  var self = this;
  self.myObject = {id: 1, value: "Hello"};
  self.openModal = function (size) {
    var modalInstance = $uibModal.open({
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl as modal',
      size: size,
      resolve: {
        // Any properties here become injected as arguments on the modal controller.
        items: function () {
          return self.myObject;
        }
      }
    });
  };
});

// items is injected from the resolve property of the parent controller instantiating the modal.
myApp.controller('ModalInstanceCtrl', function(items) {
  var self = this;
  self.myObject = items;
});

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

Remove controller option from your $modal.open options, which is creating new controller instance for the modal popup & ignoring scope : $scope option of modal options while opening popup.

var modalInstance = $uibModal.open({
    animation : $scope.animationsEnabled,
    templateUrl : 'partials/operation.html',
    //controller : 'ProductsController', //<-- remove controller name
    scope : $scope,
    size : size
});

Upvotes: 2

Related Questions