角度確認已包含一個屬性顯示或隱藏確認信息。
<button type="button" ng-click="delete()" confirm-if="checked" confirm="Are you sure, {{name}}?">Delete</button>
我做了該插件的一些修改上添加一條消息「最後一次確認將顯示」
1)在HTML中confirm-last
attribut將呈現顯示在確認模式上的警告文本:
<button class="btn btn-lg btn-invisible" type="button" ng-click="add(id)"
confirm="add"
confirm-if="showconfirm.add<=showconfirm.max"
confirm-last="showconfirm.add==showconfirm.max" >
Add
</button>
<button class="btn btn-lg btn-invisible" type="button" ng-click="edit(id)"
confirm="edit"
confirm-if="showconfirm.edit<=showconfirm.max"
confirm-last="showconfirm.edit==showconfirm.max" >
Add
</button>
<button class="btn btn-lg btn-invisible" type="button" ng-click="clone(id)"
confirm="clone"
confirm-if="showconfirm.clone<=showconfirm.max"
confirm-last="showconfirm.clone==showconfirm.max" >
Add
</button>
2)在所述控制器
$scope.showconfirm = {
"max" : 2, // how much time the confirm will show for an action
"add" : 0, // how many time it actually show the confirm for this action
"edit" : 0,
"clone": 0
} ;
$scope.add = function(id) {
$scope.showconfirm.add ++;
// ...
}
$scope.edit = function(id) {
$scope.showconfirm.edit ++;
// ...
}
3)更改模塊
angular.module('interfaceApp').run(function($confirmModalDefaults) {
$confirmModalDefaults.templateUrl = "partials/elements/confirm.html";
$confirmModalDefaults.defaultLabels.title = 'Confirmez';
$confirmModalDefaults.defaultLabels.ok = 'Oui';
$confirmModalDefaults.defaultLabels.cancel = 'Non';
$confirmModalDefaults.defaultLabels.confirmLastMessage = "(Dernière confirmation pour cette action)";
});
4)的默認PARAM創建HTML新模板confirm.html
<div class="modal-header"><h3 class="modal-title">{{data.title}}</h3></div>
<div class="modal-body">{{data.text}}</div>
<div class="modal-footer no-topbottom-padding">
<div ng-show="data.confirmLast" style="float:left;padding: 10px;color:#FF0000">
<i>{{data.confirmLastMessage}}</i>
</div>
<div style="float: right;padding: 10px;">
<button class="btn btn-primary" ng-click="ok()">{{data.ok}}</button>
<button class="btn btn-default" ng-click="cancel()">{{data.cancel}}</button>
</div>
</div>
5)所編輯的模塊(編輯部分標有**edited her**
):
/*
* angular-confirm
* https://github.com/Schlogen/angular-confirm
* @version v1.2.4 - 2016-05-11
* @license Apache
*/
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports = factory(require('angular'));
} else {
return factory(root.angular);
}
}(this, function (angular) {
angular.module('angular-confirm', ['ui.bootstrap.modal'])
.controller('ConfirmModalController', function ($scope, $uibModalInstance, data) {
$scope.data = angular.copy(data);
$scope.ok = function (closeMessage) {
$uibModalInstance.close(closeMessage);
};
$scope.cancel = function (dismissMessage) {
if (angular.isUndefined(dismissMessage)) {
dismissMessage = 'cancel';
}
$uibModalInstance.dismiss(dismissMessage);
};
})
.value('$confirmModalDefaults', {
template: '<div class="modal-header"><h3 class="modal-title">{{data.title}}</h3></div>' +
'<div class="modal-body">{{data.text}}</div>' +
'<div class="modal-footer">' +
'<button class="btn btn-primary" ng-click="ok()">{{data.ok}}</button>' +
'<button class="btn btn-default" ng-click="cancel()">{{data.cancel}}</button>' +
'</div>',
controller: 'ConfirmModalController',
defaultLabels: {
title: 'Confirm',
ok: 'OK',
cancel: 'Cancel'
}
})
.factory('$confirm', function ($uibModal, $confirmModalDefaults) {
return function (data, settings) {
var defaults = angular.copy($confirmModalDefaults);
settings = angular.extend(defaults, (settings || {}));
data = angular.extend({}, settings.defaultLabels, data || {});
if ('templateUrl' in settings && 'template' in settings) {
delete settings.template;
}
settings.resolve = {
data: function() {
return data;
}
};
return $uibModal.open(settings).result;
};
})
.directive('confirm', function ($confirm, $timeout) {
return {
priority: 1,
restrict: 'A',
scope: {
confirmIf: "=",
confirmLast: "=", // **edited her**
ngClick: '&',
confirm: '@',
confirmSettings: "=",
confirmTitle: '@',
confirmOk: '@',
confirmCancel: '@'
},
link: function (scope, element, attrs) {
function onSuccess() {
var rawEl = element[0];
if (["checkbox", "radio"].indexOf(rawEl.type) != -1) {
var model = element.data('$ngModelController');
if (model) {
model.$setViewValue(!rawEl.checked);
model.$render();
} else {
rawEl.checked = !rawEl.checked;
}
}
scope.ngClick();
}
element.unbind("click").bind("click", function ($event) {
$event.preventDefault();
$timeout(function() {
if (angular.isUndefined(scope.confirmIf) || scope.confirmIf) {
var data = {text: scope.confirm};
// **edited her**
if (scope.confirmLast) {
data.confirmLast = scope.confirmLast;
}
if (scope.confirmTitle) {
data.title = scope.confirmTitle;
}
if (scope.confirmOk) {
data.ok = scope.confirmOk;
}
if (scope.confirmCancel) {
data.cancel = scope.confirmCancel;
}
$confirm(data, scope.confirmSettings || {}).then(onSuccess);
} else {
scope.$apply(onSuccess);
}
});
});
}
}
});
}));
謝謝,我會試試這個 – AlainIb
我應該學會閱讀...指令已經包含一個參數('confirm-if')來顯示或不顯示確認:''。 – AlainIb