如何在角度?弹出模型的控制器中访问ng-model值?

时间:2015-06-03 09:59:04

标签: angularjs angularjs-scope

我想从控制器中访问ngModel,但这里的ngModel是在弹出输入字段中定义的。我想访问控制器中的数量和名称值。请注意,整个代码是模型弹出窗口。

型号代码

.controller('GuestDetailsCtrl', function($scope){
    $scope.addItem = function() {
        alert($scope.name);
        alert($scope.qty);
    }; 
});

控制器代码

{{1}}

1 个答案:

答案 0 :(得分:7)

您可以尝试在函数中给出params:

<ion-modal-view> 
<ion-header-bar>
    <h1 class="title">Item Details</h1>
</ion-header-bar>
<ion-content padding="true">
    <form ng-submit="addItem(params)">
        <div class="list list-inset">
            <label class="item item-input">
                <span class="input-label">Name</span>
                <input type="text" name="name" ng-model="params.name">
            </label>
            <label class="item item-input">
                <span class="input-label">Qty</span>
                <input type="number" name="qty" ng-model="params.qty">
            </label>
            <div class="padding item text-center">
                <button class="button button-dark">Add To Cart</button>
                <a class="button  button-assertive" ng-click="closeModal()">Cancel</a>            
            </div>
        </div>
    </form>
</ion-content>

在你的控制器中:

   .controller('GuestDetailsCtrl', function($scope){
      $scope.addItem = function(params) {
          alert(params.name);
          alert(params.qty);
      }; 
  });