AngularJS+BootStrap实现弹出对话框

参考资料:

http://angular-ui.github.io/bootstrap/#/modal

https://www.zybuluo.com/bornkiller/note/6023

http://www.html5jq.com/fe/angular_node/20141127/13.html

若想要实现对话框弹出效果,可以使用angular的$modal服务。Github上的解释如下:

$modal is a service to quickly create AngularJS-powered modal windows. Creating custom modals is straightforward: create a partial view, its controller and reference them when using the service.

The $modal service has only one method: open(options) where available options are like follows:

templateUrl - a path to a template representing modal's content

template - inline template representing the modal's content

scope - a scope instance to be used for the modal's content (actually the $modal service is going to create a child scope of a provided scope). Defaults to $rootScope

controller - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax in the form 'SomeCtrl as myctrl'; can be injected with $modalInstance

controllerAs - an alternative to the controller-as syntax, matching the API of directive definitions. Requires the controller option to be provided as well

bindToController - when used with controllerAs & set to true, it will bind the $scope properties onto the controller directly

resolve - members that will be resolved and passed to the controller as locals; it is equivalent of the resolve property for AngularJS routes

animation - set to false to disable animations on new modal/backdrop. Does not toggle animations for modals/backdrops that are already displayed.

backdrop - controls presence of a backdrop. Allowed values: true (default), false (no backdrop), 'static' - backdrop is

present but modal window is not closed when clicking outside of the modal window.

keyboard - indicates whether the dialog should be closable by hitting the ESC key, defaults to true

backdropClass - additional CSS class(es) to be added to a modal backdrop template

windowClass - additional CSS class(es) to be added to a modal window template

windowTemplateUrl - a path to a template overriding modal's window template

size - optional suffix of modal window class. The value used is appended to the modal- class, i.e. a value of sm gives modal-sm

openedClass - class added to the body element when the modal is opened. Defaults to modal-open

Global defaults may be set for $modal via $modalProvider.options.

The open method returns a modal instance, an object with the following properties:

close(result) - a method that can be used to close a modal, passing a result

dismiss(reason) - a method that can be used to dismiss a modal, passing a reason

result - a promise that is resolved when a modal is closed and rejected when a modal is dismissed

opened - a promise that is resolved when a modal gets opened after downloading content's template and resolving all variables

rendered - a promise that is resolved when a modal is rendered.

In addition the scope associated with modal's content is augmented with 2 methods:

$close(result)

$dismiss(reason)

Those methods make it easy to close a modal window without a need to create a dedicated controller.

If the $scope is destroyed via unexpected mechanism, such as it being passed in the modal options and a $route/$state transition occurs,

the modal will be dismissed with the value $uibUnscheduledDestruction.

Finally, a modal.closing event is broadcast to the modal scope before the modal closes. If the listener calls preventDefault on the event,

then the modal will remain open. The $close and $dismiss methods return true if the event was allowed.

The event itself includes a parameter for the result/reason and a boolean parameter that indicates whether the modal is being closed (true) or dismissed.

译文如下:

$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们。

$modal仅有一个方法open(options),可以使用的选项如下:

templateUrl:模态窗口的地址

template:用于显示html标签

scope:一个作用域为模态的内容使用(事实上,$modal会创建一个当前作用域的子作用域)默认为$rootScope

controller:为$modal指定的控制器,初始化$scope,该控制器可用$modalInstance注入

resolve:定义一个成员并将他传递给$modal指定的控制器,相当于routes的一个reslove属性,如果需要传递一个objec对象,需要使用angular.copy()

backdrop:控制背景,允许的值:true(默认),false(无背景),“static” - 背景是存在的,但点击模态窗口之外时,模态窗口不关闭

keyboard:当按下Esc时,模态对话框是否关闭,默认为ture

windowClass:指定一个class并被添加到模态窗口中

open方法返回一个模态实例,该实例有如下属性

close(result):关闭模态窗口并传递一个结果

dismiss(reason):撤销模态方法并传递一个原因

result:一个契约,当模态窗口被关闭或撤销时传递

opened:一个契约,当模态窗口打开并且加载完内容时传递的变量

另外,$modalInstance扩展了两个方法$close(result)、$dismiss(reason),这些方法很容易关闭窗口并且不需要额外的控制器

实战:

在app.js中需要加入依赖ui.bootstrap,需要在index.html中引入ui-bootstrap-tpls-0.7.0.js。

以下为html代码:

<div ng-controller="ModalDemoCtrl">
<span style="white-space:pre">	</span><script type="text/ng-template" id="myModalContent.html">
<span style="white-space:pre">	</span><div class="modal-header">
<span style="white-space:pre">		</span><h3 class="modal-title">I'm a modal!</h3>
<span style="white-space:pre">	</span></div>
<span style="white-space:pre">	</span><div class="modal-body">
<span style="white-space:pre">		</span><ul>
<span style="white-space:pre">		</span>     <li ng-repeat="item in items">
<span style="white-space:pre">	</span>                    <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> 
                     </li>
                </ul>
              Selected: <b>{{ selected.item }}</b>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
            <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
        </div>
    </script>
    <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
    <button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
    <button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
    <button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button>
    <div ng-show="selected">Selection from a modal: {{ selected }}</div></div>
     angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {
       $scope.items = ['item1', 'item2', 'item3'];
       $scope.animationsEnabled = true;
       $scope.open = function (size) {
         var modalInstance = $modal.open({
               animation: $scope.animationsEnabled,
               templateUrl: 'myModalContent.html',
               controller: 'ModalInstanceCtrl',
               size: size,
               resolve: {
               items: function () {
                     return $scope.items;
                   } 
                 }
              });

以下为JS代码:

modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;    },
      function () {
           $log.info('Modal dismissed at: ' + new Date());    });  };
     $scope.toggleAnimation = function () {
          $scope.animationsEnabled = !$scope.animationsEnabled;  }; });
     // Please note that $modalInstance represents a modal window (instance) dependency.
     // It is not the same as the $modal service used above.
     angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
      $scope.items = items;
      $scope.selected = {
           item: $scope.items[0]  };
      $scope.ok = function () {
         $modalInstance.close($scope.selected.item);  };
     $scope.cancel = function () {    $modalInstance.dismiss('cancel');
     };});

出现如下错误:

Modal dismissed at: Mon Sep 14 2015 18:53:33 GMT+0800 (中国标准时间)

也就是说bootstrap.min.css、ui-bootstrap-tpls-0.13.4.js、angular.js要保持一定的版本关系。

在将ui-bootstrap-tpls的版本改为0.13.4之后,又出现如下错误提示:

Error: [$injector:unpr] Unknown provider: $templateRequestProvider <- $templateRequest <- $modal

如果是因为版本冲突的原因,自己就尝试着更换ui-bootstrap-tpls的版本,结果当试到ui-bootstrap-tpls-0.9.0.js时,发现问题解决了。截图如下:

在热心网友破狼的大力支持下,问题终于解决了,在此表示深深的谢意。

AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框的更多相关文章

  1. Bootstrap模态弹出窗

    Bootstrap模态弹出窗有三种方式: 1.href触发模态弹出窗元素: <a class="btn btn-primary" data-toggle="moda ...

  2. bootstrap实现弹出窗口

    bootstrap使用modal-dialog实现弹对话框. 一个对话框包含3部分: 对话框头部 modal-header 对话框内容体 modal-body 对话框底部 modal-footer 如 ...

  3. Bootstrap:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  4. JS组件Bootstrap实现弹出框和提示框效果代码

    这篇文章主要介绍了JS组件Bootstrap实现弹出框和提示框效果代码,对弹出框和提示框感兴趣的小伙伴们可以参考一下 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编 ...

  5. Bootstrap实现弹出框和提示框效果代码

    一.Bootstrap弹出框使用过JQuery UI应该知道,它里面有一个dialog的弹出框组件,功能也很丰富.与jQuery UI的dialog类似,Bootstrap里面也内置了弹出框组件.打开 ...

  6. Bootstrap模态弹出框

    前面的话 在 Bootstrap 框架中把模态弹出框统一称为 Modal.这种弹出框效果在大多数 Web 网站的交互中都可见.比如点击一个按钮弹出一个框,弹出的框可能是一段文件描述,也可能带有按钮操作 ...

  7. Bootboxjs快速制作Bootstrap的弹出框效果

    Bootboxjs是一个简单的js库,简单快捷帮你制作一个Bootstrap的弹出框效果. 一.简介 bootbox.js是一个小的JavaScript库,它帮助您在使用bootstrap框架的时候快 ...

  8. [Bootstrap]modal弹出框

    写在前面 在实际开发中,为了友好,更需要一种美观的弹出框,js原生的alert,很难满足需求.这里推荐一个bootstrap的弹出框. 一个例子 先看效果吧 代码: <!DOCTYPE html ...

  9. selenium webdriver学习(六)------------如何得到弹出窗口

    selenium webdriver学习(六)------------如何得到弹出窗口 在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理 ...

随机推荐

  1. REFRESH删除POSTGRESQL

    sudo apt-get install python-psycopg2sudo apt-get install postgresql sudo su - postgres createuser -- ...

  2. Ubuntu下装QQ2014(http://my.oschina.net/oscfox/blog/315951)

    QQ登陆界面: QQ登陆之后: 1.首先我们需要下载一个 deb的 Wine QQ安装包 qq2014官方下载:http://www.longene.org/download/WineQQ2013SP ...

  3. JDBC编程-事务编程(四)

    事务的概念 事务的概念在我看来是指的是一组sql序列,这个序列是一块执行的单位,要么全部执行,要不全部执行,这样可以很好的对数据库进行并发控制. 因为数据库是多个用户都可以同时操作的,如果多个用户同时 ...

  4. 【Netty源码分析】Reactor线程模型

    1. 背景 1.1. Java线程模型的演进 1.1.1. 单线程 时间回到十几年前,那时主流的CPU都还是单核(除了商用高性能的小机),CPU的核心频率是机器最重要的指标之一. 在Java领域当时比 ...

  5. Linux之read命令使用

    read命令: read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 1)read后面的变量var可以只有一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个 ...

  6. EBS财务模块表结构

     gl_code_combinations:科目组合 字段名 含义 备注 code_combination_id 主键,科目编码ID,自动编号 segment1 分行代码 setgment2 是受 ...

  7. [openwrt] uci 的shell和lua接口

    uci是openwrt上配置操作的接口,不管是自动化的shell脚本,还是使用luci来二次开发配置界面,都会用到这部分知识. uci提供了lua, shell, c接口,这里主要用到了前两种 she ...

  8. Android开发小问题集

    由于安卓系统比较复杂,开发中会发中会碰见各种小问题,在此做一些记录,只要觉得有必要就会添加进来. 1.触屏鼠标模式和触屏模式 开发android4.3高通400平台时,用atmel_max 640T作 ...

  9. iOS中 最新支付宝支付(AliPay) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 现在的支付方式一般有三种, 支付宝, 微信, 网银. 个人觉得最简单易用的还是支付宝, 微信虽然看起来币支付宝要简单 ...

  10. 多态原理探究-从C++编译器角度理解多态的实现原理

    理论知识: 当类中声明虚函数时,编译器会在类中生成一个虚函数表. 虚函数表是一个存储类成员函数指针的数据结构. 虚函数表是由编译器自动生成与维护的. virtual成员函数会被编译器放入虚函数表中. ...