AngularJS 中使用 AngularUI Bootstrap Modal 的范围问题 [英] Scope issue in AngularJS using AngularUI Bootstrap Modal

查看:18
本文介绍了AngularJS 中使用 AngularUI Bootstrap Modal 的范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

example.js:

angular.module('plunker', ['ui.bootstrap']);var ModalDemoCtrl = 函数($scope,$modal){$scope.open = 函数 () {var modalInstance = $modal.open({templateUrl: 'modal.html',控制器:'ModalInstanceCtrl'});};};var ModalInstanceCtrl = function ($scope, $modalInstance) {$scope.ok = 函数 () {警报($scope.text);};$scope.cancel = 函数 () {$modalInstance.dismiss('cancel');};};

index.html:

<html ng-app="plunker"><头><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"><身体><div ng-controller="ModalDemoCtrl"><button class="btn" ng-click="open()">打开我!</button><div ng-show="selected">从模态中选择:{{ selected }}</div>

</html>

modal.html:

<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()">取消</button>

为什么我无法获得 ModalInstanceCtrl 中定义的 $scope.text,即使我可以使用 $scope.ok 和 $scope.cancel?

解决方案

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

var ModalInstanceCtrl = function ($scope, $modalInstance) {$scope.input = {};$scope.ok = 函数 () {警报($scope.input.abc);};$scope.cancel = 函数 () {$modalInstance.dismiss('cancel');};};

HTML:

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

example.js:

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:

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

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

Why I can't get the $scope.text defined in ModalInstanceCtrl, even though I can use $scope.ok and $scope.cancel?

解决方案

Looks like a scope issue. I got it to work like this:

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>

这篇关于AngularJS 中使用 AngularUI Bootstrap Modal 的范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆