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


so have been reading directives, services , controllers. felt had understanding of goes where. example dom manipulation happens in directive, api calls happen in service. needed make modal. fist thought directive, looked @ angular ui , have set service. surprised see service. correct way it, or considered , anti pattern? read angular ui place when learning, i'm not sure? modal more confusing expected.

the general rule in angular dom manipulation should take place inside directives, , of time rule applies. there situations declarative approach doesn't feel right, least, because situations intrinsically imperative. modals , custom alerts 2 examples, name few.

to exemplify i'm saying, take @ example taken a similar question answered 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 else            }         });     } }); 

back in day angularui's $modal 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 else         }     } }); 

the second approach awkward write , breaks flow of code. it's trying fit square peg round hole.

imo dom manipulation happens in directive statement more (very) strong recommendation hard rule. exists people - newcomers angular - avoid accessing dom within services , controllers.