首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用AngularUI引导模式的AngularJS中的作用域问题

使用AngularUI引导模式的AngularJS中的作用域问题
EN

Stack Overflow用户
提问于 2013-09-10 18:11:33
回答 3查看 51.4K关注 0票数 53

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

example.js:

代码语言:javascript
复制
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.html:

代码语言:javascript
复制
<!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.html:

代码语言:javascript
复制
<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>

为什么我不能在ModalInstanceCtrl中定义$scope.text,即使我可以使用$scope.ok和$scope.cancel?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-10 18:29:25

看起来像是范围问题。我让它像这样工作:

代码语言:javascript
复制
var ModalInstanceCtrl = function ($scope, $modalInstance) {
    $scope.input = {};
    $scope.ok = function () {
        alert($scope.input.abc);
    };

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

HTML:

代码语言:javascript
复制
<textarea ng-model="input.abc"></textarea>
票数 72
EN

Stack Overflow用户

发布于 2014-04-14 00:04:13

更新2014年11月:该问题已使用angular-ui-bootstrap 0.12.0修复-转换范围与控制器的范围合并。不需要做任何事情。只需继续使用:

代码语言:javascript
复制
<textarea ng-model="text"></textarea>

0.12.0之前的

Angular-UI modals使用转译来附加modal内容,这意味着在modal中创建的任何新范围条目都会在子范围中创建。

您应该使用继承并初始化父$scope中的空text条目,或者可以显式地将输入附加到父作用域:

代码语言:javascript
复制
<textarea ng-model="$parent.text"></textarea>
票数 16
EN

Stack Overflow用户

发布于 2014-08-28 14:02:53

让我试着解释一下原因。ui-bootstrap模式源码:

代码语言:javascript
复制
.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';
  },

和模板源码- window.html:

代码语言:javascript
复制
<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-transclude,your对话框内容将插入其中,它的源码:

代码语言:javascript
复制
.directive('modalTransclude', function () {
return {
  link: function($scope, $element, $attrs, controller, $transclude) {
    $transclude($scope.$parent, function(clone) {
      $element.empty();
      $element.append(clone);
    });
  }
};

})

现在看看$compile的官方文档:

代码语言:javascript
复制
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将创建一个新的控制器作用域

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18716113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档