AngularJs的UI組件ui-Bootstrap分享(十)——Model


原文地址:http://www.cnblogs.com/pilixiami/p/5677515.html

Model是用來創建模態窗口的,但是實際上,並沒有Model指令,而只有$uibModal服務,創建模態窗口是使用$uibModal.open()方法。

創建模態窗口時,要有一個模態窗口的模板和對應的控制器,然后在open()方法的參數中指定它們。來看一個例子:

 

<!DOCTYPE html>
<html ng-app="ui.bootstrap.demo" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="/Content/bootstrap.css" rel="stylesheet" />
    <title></title>

    <script src="/Scripts/angular.js"></script>
    <script src="/Scripts/ui-bootstrap-tpls-1.3.2.js"></script>
    <script>
        angular.module('ui.bootstrap.demo', ['ui.bootstrap']).controller('ModalDemoCtrl', function ($scope, $uibModal, $log) {

            $scope.items = ['item1', 'item2', 'item3'];

            $scope.open = function (size) {
                var modalInstance = $uibModal.open({
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    backdrop: "static",
                    size: size,
                    resolve: {
                        items1: function () {
                            return $scope.items;
                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $scope.selected = selectedItem;
                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };

            $scope.toggleAnimation = function () {
                $scope.animationsEnabled = !$scope.animationsEnabled;
            };

        });

//$uibModalInstance是模態窗口的實例  
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items1) {
            $scope.items = items1;
            $scope.selected = {
                item: $scope.items[0]
            };

            $scope.ok = function () {
                $uibModalInstance.close($scope.selected.item);
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        });
    </script>

</head>
<body>
    <div ng-controller="ModalDemoCtrl">
        <script type="text/ng-template" id="myModalContent.html">
            <div class="modal-header">
                <h3 class="modal-title">I'm a modal!</h3>
            </div>
            <div class="modal-body">
                <ul>
                    <li ng-repeat="item in items">
                        <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>
        <div ng-show="selected">Selection from a modal: {{ selected }}</div>
    </div>
</body>
</html>

 

背景為灰色,不可操作。效果是這樣:

open()中可以使用的參數有:

參數名 默認值 備注
animation true 是否啟用動畫
appendTo  body 把模態窗口放在指定的dom元素中。例如$document.find('aside').eq(0)
backdrop  true

打開模態窗口時的背景設置。可設置的值有:true(顯示灰色背景,在模態窗口之外單擊會關閉模態窗口),false

(不顯示灰色背景),"static"(顯示灰色背景,在模態窗口關閉之前背景元素不可用)

backdropClass   為背景添加的類名
bindToController false 設置為true並且使用controllerAs參數時,$scope的屬性會傳遞給模態窗口所使用的controller
controller  

可以設置為一個表示controller的字符串,或者一個函數,或者一個數組(使用數組標記的方式為控制器注入依賴)。

控制器中可使用$uibModalInstance來表示模態窗口的實例。

controllerAs    controller-as語法的替代寫法
keyboard  true  是否允許用ESC鍵關閉模態窗口
openedClass   modal-open  打開模態窗口時為body元素增加的類名
resolve   傳遞到模態窗口中的對象
scope $rootScope 模態窗口的父作用域對象
size   一個字符串,和前綴“model-”組合成類名添加到模態窗口上
template   表示模態窗口內容的文本
templateUrl   模態窗口內容的模板url
windowClass   添加到模態窗口模板的類名(不是模態窗口內容模板)
windowTemplateUrl uib/template/modal/window.html   
windowTopClass   添加到頂層模態窗口的類名

全局的設置可以通過$uibModalProvider.options來配置。

 

使用controller-as語法時,可以為controller注冊一個別名,並且將這個controller當作一個普通的Javascript對象,不需要注入$scope,也不需要將視圖模型的內容綁定到$scope上。

有兩種方式使用controller-as語法:

1 在controller中指定controller:"ModalInstanceCtrl as vm"(不使用controllerAs屬性)

2 在controllerAs屬性中指定controllerAs:"vm"

這兩種方式的效果是一樣的。來看這個例子:

 

<!DOCTYPE html>
<html ng-app="ui.bootstrap.demo" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="/Content/bootstrap.css" rel="stylesheet" />
    <title></title>

    <script src="/Scripts/angular.js"></script>
    <script src="/Scripts/ui-bootstrap-tpls-1.3.2.js"></script>
    <script>
        angular.module('ui.bootstrap.demo', ['ui.bootstrap']).controller('ModalDemoCtrl', function ($scope, $uibModal, $log) {

            $scope.items = ['item1', 'item2', 'item3'];

            $scope.open = function (size) {
                var modalInstance = $uibModal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    controllerAs: 'vm',
                    backdrop: "static",
                    size: size,
                    resolve: {
                        items1: function () {
                            return $scope.items;
                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $scope.selected = selectedItem;
                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };

            $scope.toggleAnimation = function () {
                $scope.animationsEnabled = !$scope.animationsEnabled;
            };

        });

        //$uibModalInstance是模態窗口的實例 
 angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, items1) {
            this.items = items1;
            this.selected = {
                item: this.items[0]
            };

            this.ok = function () {
                $uibModalInstance.close(this.selected.item);
            };

            this.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };

        });
    </script>

</head>
<body>
    <div ng-controller="ModalDemoCtrl">
        <script type="text/ng-template" id="myModalContent.html">
            <div class="modal-header">
                <h3 class="modal-title">I'm a modal!</h3>
            </div>
            <div class="modal-body">
                <ul>
                    <li ng-repeat="item in vm.items">
                        <a href="#" ng-click="$event.preventDefault(); vm.selected.item = item">{{ item }}</a>
                    </li>
                </ul>
                Selected: <b>{{ vm.selected.item }}</b>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="button" ng-click="vm.ok()">OK</button>
                <button class="btn btn-warning" type="button" ng-click="vm.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>
        <div ng-show="selected">Selection from a modal: {{ selected }}</div>
    </div>
</body>
</html>

 

這個例子中,ModalInstanceCtrl的別名是vm,ModalInstanceCtrl沒有注入$scope,所有的屬性都使用this綁定到controller對象本身,在視圖中使用vm.Items或者vm.ok()來調用controller的對象

 

$uibModal.open()方法返回一個模態窗口實例,這個實例有幾個屬性:

屬性名 類型 說明
close(result) function 關閉模態窗口,傳遞一個結果
dismiss(reason)  function 取消模態窗口,傳遞一個原因
result promise 一個promise,窗口關閉時為resolved,窗口取消時為rejected
opened promise 一個promise,窗口打開並下載完內容解析了所有變量后,promise為resolved
closed promise 一個promise,窗口關閉並且動畫結束后為resolved
rendered promise 一個promise,窗口呈現出來后為resolved

除了可以使用模態窗口的實例來關閉和取消窗口(上面例子中的$uibModalInstance.close($scope.selected.item);),和模態窗口關聯的scope也可以關閉和取消窗口。如:

 

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items1) {
             $scope.items = items1;
             $scope.selected = {
                 item: $scope.items[0]
             };
 
             $scope.ok = function () {
                 $scope.$close("aa");
             };
 
             $scope.cancel = function () {
                 $scope.$dismiss("cancel");
             };
         });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM