ocombe / ocModal

An angularJS modal directive & service
66 stars 16 forks source link

ocModal

An angularJS modal directive & service

Key features

Demo on Plunker

Usage

Service:

$ocModal.open('partials/modal.html');

or

$ocModal.open('<div>My content</div>');

Directive:

<div oc-modal-open="'partials/modal.html'"></div>

or

<div oc-modal-open="'<div>My content</div>'"></div>

See the example in the 'example' folder to know how to integrate ocLazyLoad with your router.

Parameters

You can also pass parameters when you open a modal via the service or the directive. The previous examples are equivalent to :

Service:

$ocModal.open({
    url: 'partials/modal.html'
});

or

$ocModal.open({
    template: '<div>My content</div>'
});

Directive:

<div oc-modal-open="{url: 'partials/modal.html'}"></div>

or

<div oc-modal-open="{template: '<div>My content</div>'}"></div>

The complete list of parameters is :

And in your controller :

angular.module('app').controller('MyController', ['$scope', '$init', function($scope, $init) {
    console.log($scope.param1, $init.param1);
}]);

And use $init in your controller :

angular.module('app').controller('MyController', ['$scope', '$init', function($scope, $init) {
    console.log($init.param1);
}]);

But $scope.param1 will be undefined.

Functions & attributes

With no parameter it will close the last opened modal. If you want to close a specific modal, use the id.

$ocModal.close('modal1');

You can also pass what you want to the onClose callback (if you have one) :

$ocModal.open({
    url: 'partials/modal.html',
    onClose: function(a, b, c) {
        console.log(a); // arg1
        b(); // whatever
        console.log(c); // {p1: 'test'}
    }
});

$ocModal.close('arg1', function() { console.log('whatever') }, {p1: 'test'});

Directives

Animations

You can use a set of animations by including the file ocModal.animations.css and by adding one of those classes with the cls parameter :

oc-modal-open="{url: 'partials/modal.html', cls: 'fade-in'}"

You can add your own animations by adding new styles to .modal .modal-dialog .modal-content and .modal .modal-dialog .modal-content.opened.