双向绑定在UI模式中不起作用

时间:2016-08-10 16:59:34

标签: javascript angularjs angular-ui-bootstrap

我试图以模态显示数据,但我似乎无法实现双向绑定。

在我的HTML中我有

<div class="cta-actions">
  <div class="action"><a class="btn btn-lg btn-max">Send a package</a></div>
  <div class="action"><a id="estimate-modal-trigger" ng-click="openPriceEstimateModal()" class="btn btn-lg">Get an estimate</a></div>
</div>

在控制器中

$scope.openPriceEstimateModal = function() {
        var modalInstance = $uibModal.open({
          animation: true,
          templateUrl: '/app/tpls/partials/estimate.modal.html',
          windowClass: 'price-estimate-modal',
          controller: 'EstimateModalCtrl'
       });
    };

模态的控制器

controller('EstimateModalCtrl', function($scope, $timeout, $uibModalInstance) {
  $scope.btnText = "Estimate"
  $scope.data = {
    pickup_address: null,
    delivery_address: null,
    cost: 0
  };

  $scope.address = {}
});

模态模板(它在玉中)

.modal-header
.modal-body
  .estimate-price-display
     h1
       span.currency &#8358;
       span.value(ng-bind="data.cost")

  .estimate-form
    form(name="form" no-validate)
      .control-group
        label Pickup Address
        input.form-control(pac type="text" name="pickup" placeholder="Enter pickup address" ng-model="data.pickup_address" ng-required="true" details="address.pickup")

      .control-group
        label Delivery Address
        input.form-control(pac type="text" name="delivery" placeholder="Enter delivery address" ng-model="data.delivery_address" ng-required="data.pickup_address" details="address.delivery")

      .control-group(style="text-align: center;")
        .button.btn.btn-lg.btn-clr-white(type="button" ng-click="getEstimate()" ng-disabled="form.$invalid" ng-bind="btnText")

打开模态时,属性会正确绑定并显示,但为$scope.btnText$scope.data.cost指定不同的值并不反映在模态模板上。但是将$ scope记录到控制台会显示已进行的更改。

我只是想知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试将$ scope传递给Modal实例。如下所述:

$scope.openPriceEstimateModal = function() {
     var modalInstance = $uibModal.open({
          animation: true,
          scope : $scope,
          templateUrl: '/app/tpls/partials/estimate.modal.html',
          windowClass: 'price-estimate-modal',
          controller: 'EstimateModalCtrl'
      });
};

默认情况下,传递给模态实例的范围将是$ rootScope,因此您必须使用$ scope覆盖它。