2014-03-19 33 views
0

我正在使用角引导模式。AngularJS引导模式范围值不显示

termsText已填充且scope.productTerms确实包含值。但由于某种原因,当我在模态内输出{{ productTerms }}时,该值未被显示。为什么?

JS

$scope.openProductTerms = function (termsText) { 
    $scope.productTerms = termsText <-- has a value in console.log() 
    var modalInstance = $modal.open({ 
     templateUrl: 'myModalTerms.html', 
     controller: ModalInstanceCtrl 
}); 




var ModalInstanceCtrl = function ($scope, $modalInstance) { 
    $scope.ok = function() { 
     $modalInstance.dismiss('OK'); 
    }; 
}; 

HTML

{{ productTerms }} < ==== value shows outside modal 


<script type="text/ng-template" id="myModalTerms.html"> 
    <div class="modal-body"> 

     {{ productTerms }} <==== same value does not show insdie modal? 

    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-primary" ng-click="ok()">OK</button> 
    </div> 
</script> 
+0

模态有一个新的控制器,并因此新的范围。这可能会有所帮助https://egghead.io/lessons/angularjs-sharing-data-between-controllers – user3360944

+0

当您打开模式时,您应该能够指定范围。 –

+0

@Zack Argyle范围是空的。 – Prometheus

回答

1

该模式有一个新的控制器,因此一个新的范围。 services.js

angular.module('services',[]). 
    factory('sharedService', function($rootScope) { 
    var sharedService = {}; 
    sharedService.value = null; 

    sharedService.prepForBroadcast = function(value) { 
     this.value = value; 
     this.broadcastItem(); 
    }; 

    sharedService.broadcastItem = function() { 
     $rootScope.$broadcast('shared'); 
    }; 

    return sharedService; 
}); 

controller.js

 $scope.openProductTerms = function (termsText) { 
      sharedService.prepForBroadcast(termsText); 
     var modalInstance = $modal.open({ 
     templateUrl: 'myModalTerms.html', 
     controller: ModalInstanceCtrl 
    }); 

lastController.js

 var ModalInstanceCtrl = function ($scope, $modalInstance,sharedService) { 
     $scope.$on('shared', function() { 
    $scope.productTerms = sharedService.value; 
    }); 
     $scope.ok = function() { 
      $modalInstance.dismiss('OK'); 
     }; 
    }; 
1

试试你的HTML

从我的经验中使用{{$parent.productTerms}},似乎角引导模式创建一个新的范围。所以通过首先引用$parent,您将能够获得您的模型的价值。

+0

对不起,因为它不是一个孩子。 – Prometheus