当前位置: 首页 > 面试题库 >

使用AngularUI引导程序模态的AngularJS中的范围问题

翟聪
2023-03-14
问题内容

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

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


问题答案:

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

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>


 类似资料:
  • 问题内容: 我有一个从或指令属性或任何其他属性中获得的字符串,我想基于此在作用域上创建一个变量。所以: 但是,如果字符串包含一个或多个点,我想将其拆分并实际上“向下钻取”到作用域中。所以应该成为。这意味着简单版本不起作用! 在读取基于字符串的变量时,您可以通过做来获得这种行为,但是在分配值时如何实现呢? 问题答案: 我发现的解决方案是使用$ parse。 “将Angular表达式转换为函数。” 如

  • 我正在用bootstrap 5制作一个整洁的图库,我想知道如何在不使用“data-bs-...”的情况下触发一个bootstrap模式。在我的HTML中(以避免重复这些数据属性50次)。 我设法为源代码获取了完整的功能javascript,但.modal()函数和.show()函数似乎不起作用。事情是这样的: 我在最后这一行javascript上受阻了。目标:触发#GalleryModal。 感谢

  • 问题内容: 如果我有以下控制器: 读出的正确方法是什么?如果有必要定义中,也不会使它语义上不正确假设是描述相关的东西的属性,而不是? 更新: 对此进行进一步的思考,如果一个孩子有多个孩子,将会在检索上产生冲突。我的问题是,什么是访问的正确方法是从? 问题答案: AngularJS中的作用域使用原型继承,当在子作用域中查找属性时,解释器将从子对象开始查找原型链,并继续寻找父对象,直到找到该属性为止,

  • 问题内容: 有没有一种方法可以在外部设置标头? 登录用户后,我从服务器获取了Auth-Token,我需要将其作为HTTP标头添加到以下所有请求中。 问题答案: 似乎规范了键名。

  • 如何为两个数字的范围设置验证。我想将第一个数字的最大值设置为第二个数字的值,将第二个数字的最小值设置为第一个numb的值。这是我的js代码: 和超文本标记语言: 请帮忙谢谢

  • 问题内容: 在父控制器作用域中,我已定义将其设置为“ x”。然后在子范围中,我使用ngModel 进行了定义: 加载页面后,会按预期正确设置为“ x”。当我选择“ y”时,在CtrlB中$ scope会按预期给出“ y”。 但是,当我检查的范围(使用AngularJS的batarang),它给“X”。 jsFiddle:http : //jsfiddle.net/sudhh/GGKjp/2/ 预览