angular服務外掛程式1——對話方塊&提示框,angular服務外掛程式

來源:互聯網
上載者:User

angular服務外掛程式1——對話方塊&提示框,angular服務外掛程式
對話方塊服務:

( 需要調用amazeui的modal樣式 )

const angular = require("angular");const jqLite = angular.element;/** * @ngdoc service * @name commonModal * @module common.modal.service * @usage    commonModal.fromTemplateUrl('./modal.html',{        scope: $scope,    }).then(function(modal) {        $scope.modal = modal;    });     //顯示對話方塊    $scope.showModal = function() {        modal.show();    };    //關閉對話方塊    $scope.closeModal = function() {        modal.hide();    } */const angular = require('angular');const jqLite = angular.element;/** * @ngdoc service * @name commonModal * @module common.modal.service * */module.exports = angular.module('csm.common.services')    .factory('commonModal', commonModal);/* @ngInject*/function commonModal($q, $document, $compile, $rootScope) {    return {        fromTemplateUrl: fromTemplateUrl,    };    /**     * @ngdoc method     * @param url     * @param options     * @name fromTemplateUrl     * @desc add a modal in document return a promise object     */    function fromTemplateUrl(url, options) {        const defer = $q.defer();        createModal(url, options, defer);        return defer.promise;    }    /**     * @ngdoc method     * @param url     * @param options     * @param defer     */    function createModal(url, options, defer) {        // Create a new scope for the modal        const scope = options.scope && options.scope.$new() || $rootScope.$new(true);        const element = $compile(url)(scope);        const modal = new Modal(element, scope, $document);        defer.resolve(modal);    }}/** * @ngdoc constructor * @param element * @param scope * @param parent * */function Modal(element, scope, parent) {    this.element = element;    this.scope = scope;    this.parent = parent;    this._init();}Modal.prototype = {    _init: _init,    show: show,    hide: hide,};function _init() {    const self = this;    self.parent[0].body.appendChild(self.element[0]);}function show() {    const self = this;    const element = self.element;    const openModal = jqLite(element.children()[0]);    element.css('display', 'block');    element.addClass('am-active');    openModal.css('display', 'block');    openModal.css('margin-top', '-100px');    setTimeout(function () {        openModal.removeClass('am-modal-out').addClass('am-modal-active');    }, 100);}function hide() {    const self = this;    const element = self.element;    const openModal = jqLite(element.children()[0]);    openModal.removeClass('am-modal-active').addClass('am-modal-out');    setTimeout(function () {        openModal.css('display', 'none');        element.css('display', 'none');        element.removeClass('am-active');    }, 200);}
提示框服務:

提示框是對對話方塊的封裝,包括success,info,warning,danger,confirm對話方塊:

const angular = require('angular');const url = require('./modal.html');/** * @ngdoc service * @name commonModal * @module common.modal.service * @usage    try {        // ...........    }    catch(err) {        AlertService.warning(err.data);    } */ module.exports = angular.module('csm.common.services')    .factory('AlertService', AlertService);/* @ngInject */function AlertService($rootScope, commonModal) {    const scope = $rootScope.$new(true);    let modal;    initModal();    return {        success: success,        info: info,        warning: warning,        danger: danger,        confirm: confirm,    };    function initModal() {        commonModal.fromTemplateUrl(url, {scope: scope}).then(function (md) {            scope.modal = modal = md;        });    }    function success(content) {        scope.texts = {            title: '成功',            body: content,            closeButton: '確定',            noDismiss: true,        };        modal.show();    }    function info(content) {        scope.texts = {            title: '提示',            body: content,            closeButton: '確定',            noDismiss: true,        };        modal.show();    }    function warning(content, callback = angular.noop) {        scope.texts = {            title: '警告',            body: content,            closeButton: '確認',            dismissButton: '關閉',            noDismiss: true,        };        modal.callback = function () {            modal.hide();            callback();        };        modal.show();    }    function danger(content, callback) {        scope.texts = {            title: '危險',            body: content,            closeButton: '我瞭解',            dismissButton: '取消',            noDismiss: false,        };        modal.callback = function () {            modal.hide();            callback();        };        modal.show();    }    function confirm(content, callback) {        scope.texts = {            title: '你確定嗎?',            body: content,            closeButton: '我確定',            dismissButton: '再想想',            noDismiss: false,        };        modal.callback = function () {            modal.hide();            callback();        };        modal.show();    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.