AngularJS ModalService for the semantic-UI framework. Have a look at the demo.
load the script to your page
reference the module as a dependency to your angular app:
angular.module('app',
[
'semantic.modals',
// other dependencies
]);
In your controller, use the ModalService
:
angular
.module( 'app' )
.controller( 'MainCtrl', function(ModalService){
var self = this;
self.confirmModal = function(){
ModalService.showModal({
title : "confirm deletion",
text: "are you sure ? The action cannot be undone",
positive: "yep",
negative: "cancel",
basic: true
}).then( function( result ){
if(result.status){
console.log("deleting item...");
}
});
};
} );
The result is :
bower install semantic-modals
showModals
accepts the following options:
ng-include
internally),ng-template
, to use only when you need your own modal code,templateId
(note: if your template file contains only one template and the template ID is equal to the template url, you can
leave this option blank),inputs.myVar
or inputs.myFunc()
. But don't forget the modal is in an isolated scope,The showModal
function returns a promise
, which is resolved upon modal close and rejected in case the modal could not be instanciated.
The resolve callback takes one argument: {status: true/false, inputs: yourInputs}
. The status
is true
if the use pressed the positive button, false
otherwise.
The inputs
argument is passed back and can have been modified. It is useful for dialogs with forms for example.
A confirm modal with html and inputs
binding, no cancel button:
ModalService.showModal( {
title: "confirm",
html: "are you sure you want to delete {{inputs.sensor.name}} ({{inputs.sensor.id}} ) ?",
positive: "yes",
basic: false,
inputs : {
sensor: {id: 3, name: "merde"}
},
cancelable : true
} ).then( function( result ){
console.log( result );
}, function(){
console.log( "error" );
} );
A modal using a partial:
ModalService.showModal( {
title: "edit",
htmlInclude: "html/_editModalContent.html",
positive: "yes",
negative: "nevermind",
basic: false,
cancelable : false
} ).then( function( result ){
console.log( result );
}, function(){
console.log( "error" );
} );
// html/_editModalContent.html
<form name="editform" class="ui form" novalidate>
<div class="ui labeled input">
<!--name-->
<div class="ui label">
name
</div>
<input type="text" ng-model="inputs.name">
</div>
<!--description-->
<div class="field">
<label>Description</label>
<textarea ng-model="inputs.description" rows="2"></textarea>
</div>
<!--location-->
<div class="field">
<label>Location</label>
<textarea ng-model="inputs.location" rows="2"></textarea>
</div>
</form>
A completely custom template:
<script type="text/ng-template" id="semanticModal.html">
<div class="ui modal small">
<i class="close icon" ng-click="close(false)"></i>
<div class="header">
edit {{inputs.sensor.id}}
</div>
<div class="image content">
<div class="description">
<form name="editform" class="ui form" novalidate>
<div class="ui labeled input">
<!--name-->
<div class="ui label">
name
</div>
<input type="text" ng-model="inputs.sensors.name">
</div>
<!--description-->
<div class="field">
<label>Description</label>
<textarea ng-model="inputs.sensor.description" rows="2"></textarea>
</div>
<!--location-->
<div class="field">
<label>Location</label>
<textarea ng-model="inputs.sensor.description" rows="2"></textarea>
</div>
</form>
</div>
</div>
<div class="actions">
<div class="ui black deny button" ng-click="close(false)">
Nope
</div>
<div class="ui positive right labeled icon button" ng-click="close(true)">
Yep, that's me
<i class="checkmark icon"></i>
</div>
</div>
</div>
</script>
If the template is located in a file with the path identical to the template id (semanticModal.html
):
ModalService.showModal( {
templateId: "semanticModal.html",
cancelable : false
} );
If the template is in a file different from the id, say partial/templates.html
:
ModalService.showModal( {
templateId: "semanticModal.html",
templateUrl: "partial/templates.html",
cancelable : false
} );