Scope issue in AngularJS using AngularUI Bootstrap Modal

后端 未结 3 994
独厮守ぢ
独厮守ぢ 2020-11-28 06:36

plunker: http://plnkr.co/edit/wURNg8ByPYbEuQSL4xwg

example.js:

angular.module(\'plunker\', [\'ui.bootstrap\']);
  var ModalDemoCtrl          


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 07:03

    Let'me try to explain the reason. ui-bootstrap modal sourcecode:

    .directive('modalWindow', ['$modalStack', '$timeout', function ($modalStack, $timeout) {
    return {
      restrict: 'EA',
      scope: {
        index: '@',
        animate: '='
      },
      replace: true,
      transclude: true,
      templateUrl: function(tElement, tAttrs) {
        return tAttrs.templateUrl || 'template/modal/window.html';
      },
    

    and the template sourcecode - window.html:

    there is a directive modal-transclude,your dialog content will insert into it, it's sourcecode:

    .directive('modalTransclude', function () {
    return {
      link: function($scope, $element, $attrs, controller, $transclude) {
        $transclude($scope.$parent, function(clone) {
          $element.empty();
          $element.append(clone);
        });
      }
    };
    

    })

    now take a look at offical doc of $compile:

    Transclusion Functions
    
    When a directive requests transclusion, the compiler extracts its contents and provides 
    a transclusion function to the directive's link function and controller. 
    This transclusion function is a special linking function that will return the compiled 
    contents linked to a **new transclusion scope.**
    

    transclude will create a new scope of controller scope

提交回复
热议问题