我失去了我的$ scope值

时间:2016-03-27 18:53:50

标签: javascript angularjs

我还是新的angularJs我试图将一个变量的值从一个页面html传递到另一个页面(实际上是一个模态)。这两个页面共享同一个控制器。请帮我诊断一下pb

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

};

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

3 个答案:

答案 0 :(得分:2)

从您的controller选项中删除option $modal.open,这会为模式弹出窗口创建新的控制器实例&amp;在打开弹出窗口时忽略模态scope : $scope的{​​{1}}选项。

options

答案 1 :(得分:1)

以下是Bootstrap模式的完整示例,并将变量从父级传递给模态控制器。 Plunker Here

<强>的index.html

<!DOCTYPE html>
<html>

  <head>
    <link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
    <script data-require="angularjs@1.5.0" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
    <script data-require="angular-ui-bootstrap@1.1.2" 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;
});

答案 2 :(得分:0)

感谢您的帮助!我可以解决问题,我不得不在我的模态中定义控制器