Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angularjs should a modal be a service or a directive?

Tags:

angularjs

So I have been reading about directives, services and controllers. I felt like I had a good understanding of what goes where. For example dom manipulation happens in a directive, api calls happen in a service. Then I needed to make a modal. My fist thought was this is a directive, then I looked at Angular UI and they have it set up as a service. I was surprised to see it as a service. Is this the correct way to do it, or is this considered and anti pattern? I read that Angular UI is a good place to look when learning, but I'm not sure? A modal is more confusing then I expected.

like image 849
Jared Christensen Avatar asked Apr 27 '15 21:04

Jared Christensen


People also ask

What is a modal in AngularJS?

From an AngularJS perspective, a modal window is nothing more than a Controller than manages a View-Model, interacts with Services, and is rendered by a View. The same it true for every other UI-oriented component in your AngularJS application.

What is modal service?

Modal service facilitates communication between on-page components and modal components. Modal service also maintains an active list of available on-page modals and the methods for interacting with them.


1 Answers

The general rule in Angular is that DOM manipulation should take place only inside directives, and most of the time that rule applies. But there are some situations where a declarative approach doesn't feel right, to say the least, because those situations are intrinsically imperative. Modals and custom alerts are two examples, to name a few.

To exemplify what I'm saying, take a look at this example taken from a similar question I answered some time ago:

Imperative approach

app.controller('Ctrl', function($scope, $dialog) {
    $scope.doSomething = function() {
        $dialog.dialog().open().then(function(result) {
           if (result === 'OK') {
               // Process OK
           }
           else {
               // Process anything else
           }
        });
    }
});

Back in the day AngularUI's $modal was called $dialog.

Declarative approach

<dialog visible="dialogVisible" callback="dialogCallback()"></dialog>

...

app.controller('Ctrl', function($scope) {
    $scope.doSomething = function() {    
        $scope.dialogVisible = true; 
    }

    $scope.dialogCallback = function(result) {
        if (result === 'OK') {
            // Process OK
        }
        else {
           // Process anything else
        }
    }
});

The second approach is awkward to write and it breaks the flow of the code. It's like trying to fit a square peg into a round hole.

IMO the DOM manipulation only happens in a directive statement is more like a (very) strong recommendation than a hard rule. It exists so people - especially newcomers to Angular - avoid accessing the DOM from within services and controllers.

like image 55
Michael Benford Avatar answered Nov 10 '22 08:11

Michael Benford