简体   繁体   中英

Two-way binding not working in UI Modal

Am trying to show data in a modal, but I can't seem to get two-way binding working.

In my HTML I have

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

In the controller

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

The controller for the modal

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

  $scope.address = {}
});

The modal template(it's in jade)

.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")

When the modal is opened the properties bind properly and are displayed, however assigning a different value to $scope.btnText or $scope.data.cost does not reflect on the modal template. But logging the $scope to console shows the changes are made.

Am just wondering if I am doing something wrong?

Try to pass the $scope to the Modal instance. Like mentioned below:

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

By default the scope passed to the modal instance will be $rootScope, so you have to override that with $scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM