source

각도에서의 스코프 문제각도 사용 JSUI 부트스트랩모달

bestscript 2023. 2. 12. 18:07

각도에서의 스코프 문제각도 사용 JSUI 부트스트랩모달

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

example.syslog:

angular.module('plunker', ['ui.bootstrap']);
  var ModalDemoCtrl = function ($scope, $modal) {

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

var ModalInstanceCtrl = function ($scope, $modalInstance) {

  $scope.ok = function () {
    alert($scope.text);
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
};

index.syslog:

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
  </head>
  <body>

  <div ng-controller="ModalDemoCtrl">
    <button class="btn" ng-click="open()">Open me!</button>
    <div ng-show="selected">Selection from a modal: {{ selected }}</div>
  </div>
 </body>
</html>

modal.modal:

<div class="modal-header">
    <h3>I'm a modal!</h3>
</div>
<textarea ng-model="text"></textarea>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>

왜 내가 $scope를 얻을 수 없는지.$scope.ok 및 $scope.cancel을 사용할 수 있는데 ModalInstanceCtrl에 정의된 텍스트?

스코프의 문제인 것 같습니다.이렇게 작동시켰어요.

var ModalInstanceCtrl = function ($scope, $modalInstance) {
    $scope.input = {};
    $scope.ok = function () {
        alert($scope.input.abc);
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
};

HTML:

<textarea ng-model="input.abc"></textarea>

2014년 11월 업데이트: angular-ui-bootstrap 0.12.0에서 문제가 해결되었습니다.- 트랜스클루전 범위가 컨트롤러의 범위와 병합되었습니다.아무것도 할 필요가 없다.계속하세요:

<textarea ng-model="text"></textarea>

0.12.0 이전 버전:

각도 UI 모달은 변환을 사용하여 모달 내용을 첨부합니다. 즉, 모달 내에서 작성된 모든 새로운 스코프 엔트리가 하위 스코프에서 작성됨을 의미합니다.

상속을 사용하고 빈 상태로 초기화해야 합니다.text부모 엔트리$scope또는 입력을 명시적으로 부모 스코프에 부가할 수 있습니다.

<textarea ng-model="$parent.text"></textarea>

이유를 설명하겠습니다.ui-bootstrap 모달 소스 코드:

.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';
  },

및 템플릿 sourcecode - window.display:

<div tabindex="-1" role="dialog" class="modal fade" ng-class="{in: animate}" ng-style="{'z-index': 1050 + index*10, display: 'block'}" ng-click="close($event)">
<div class="modal-dialog" ng-class="{'modal-sm': size == 'sm', 'modal-lg': size == 'lg'}"><div class="modal-content" modal-transclude></div></div>

대화상자 콘텐츠가 삽입되는 명령어 modal-exclude가 있습니다.그것은 소스 코드입니다.

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

})

이제 $120의 공식 문서를 살펴보십시오.

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는 컨트롤러 범위의 새로운 범위를 만듭니다.

언급URL : https://stackoverflow.com/questions/18716113/scope-issue-in-angularjs-using-angularui-bootstrap-modal