Shall I use "directive" or "controller" for this case?

Freewind

We want to write a dialog directive in an angularjs application, but have something hard to decide.

Say I have some buttons on the HTML and we want to write a directive 'popup':

<div>
  <button popup>Email</button>
  <button popup>Phone</button>      
  <button popup>Other</button>
</div>

When clicked on the button, it will show a popup dialog with something. There are some common UI behaviors between the 3 buttons, as well as some different logic.

option 1 (no controller)

We define 3 directives, e.g. popup-email, popup-phone, popup-other:

app.directive('popup-email', ['user', 'ngDialog', function(user, ngDialog) {
    return {
       link: function(scope, element, attrs) {
          if(user.email) {
            ngDialog.open({...});
          } else {
            element.addClass("disabled");
          }
       }
    }
}]);

app.directive('popup-phone', ['user', 'ngDialog', function(user, ngDialog) {
    return {
       link: function(scope, element, attrs) {
          if(user.phone) {
            ngDialog.open({...});
          } else {
            element.addClass("disabled");
          }
       }
    }
}]);

app.directive('popup-other', ['user', 'ngDialog', function(user, ngDialog) {
    return {
       link: function(scope, element, attrs) {
          if(user.other) {
            ngDialog.open({...});
          } else {
            element.addClass("disabled");
          }
       }
    }
}]);

HTML:

<div>
  <button popup-email>Email</button>
  <button popup-email>Phone</button>      
  <button popup-email>Other</button>
</div>

In this option, we will define 3 directives, no controller. The good part is each button is a standalone button with the directive, all the logic is inside the directive. The bad part is we have some business logic in each directive, and there are some common logic in all the 3 directives.

Option 2 (with a controller)

We will provide only one directive for the command behavior, and one controller for the difference:

app.directive('popup', ['ngDialog', function(ngDialog) {
    return {
       link: function(scope, element, attrs) {
          if(attrs.enable==="true") {
            ngDialog.open({...});
          } else {
            element.addClass("disabled");
          }
       }
    }
}]);

app.controler('ContactController', function(scope, user) {
    scope.enableEmail = if(user.email) true else false;
    scope.enablePhone = if(user.phone) true else false;
    scope.enableOther = if(user.other) true else false;
});

The HTML will be:

<div>
  <button popup enable="{{enableEmail}}">Email</button>
  <button popup enable="{{enablePhone}}">Phone</button>      
  <button popup enable="{{enableOther}}">Other</button>
</div>

For this option, the good part is the directive is reusable, but we will have to define a controller for them.

Víťa Plšek - angular.cz

Second option is wrong, its strange to pass user both to directive and controller. Also if you have it in controller I would pass it to view and determine "enable" attribute there.

But when I speak about passing "user" into directive as dependency. I am not sure if you really want to do that, Id rather pass data to directive using attribute, but i don't know your exact use case, so its up to you.

If you insist on passing user that way I would suggest more clever directive, which accept field on which is enabled.

<button popup="email">Email</button>

When using isolated scope on attribute directive, you can use that attribute also for passing string value with field name, so you can get your field and then determine what to do using

app.directive('popup', function (..) {
...
return {
   restrict : "A",
   scope : {
     field-name : "@popup"
   },
   link: function(scope, element, attrs) {
      ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shall I use static here or not?

From Dev

Shall we use noexcept everywhere in this case?

From Dev

Which annotation shall I use to keep spring boot from securing my Controller when actuator is active

From Dev

Shall I use POJO or JSONObject for REST calls

From Dev

animate menu: shall I use canvas or javascript?

From Dev

How shall I use `docker rm -v`?

From Dev

Cant use directive controller values in directive template

From Dev

Is it acceptable to use the !important directive in this case?

From Dev

Use parent controller required in directive

From Dev

AngularJS use Directive from controller

From Java

Shall I use WebSocket on ports other than 80?

From Dev

Which datatype shall i use to store image in database(PostgreSQL)?

From Dev

Shall I use Visual Studio Express or Visual Studio 2013?

From Dev

shall I use callback pattern in my custom function? node js

From Dev

VB.Net - when shall I use "New" word?

From Dev

Which java collection shall I use to store my data?

From Dev

Java programming to interfaces shall I use it all the time, especially for collections

From Dev

Shall I extend the service class or just use an instance of it?

From Dev

Shall I use package name or class name to reprensent a concept?

From Dev

VB.Net - when shall I use "New" word?

From Dev

shall I use callback pattern in my custom function? node js

From Dev

Use CoffeeScript's class in AngularJS directive controller

From Dev

AngularJs - Use custom filter inside directive controller

From Dev

How to use/sync data of controller in a directive?

From Dev

Dynamically specify the controller to use in an Angular directive

From Dev

Use CoffeeScript's class in AngularJS directive controller

From Dev

how to use controller $scope in directive template

From Dev

How use controller variable inside a AngularJs directive

From Dev

How to make directive use the controller specified in directive attribute?

Related Related

  1. 1

    Shall I use static here or not?

  2. 2

    Shall we use noexcept everywhere in this case?

  3. 3

    Which annotation shall I use to keep spring boot from securing my Controller when actuator is active

  4. 4

    Shall I use POJO or JSONObject for REST calls

  5. 5

    animate menu: shall I use canvas or javascript?

  6. 6

    How shall I use `docker rm -v`?

  7. 7

    Cant use directive controller values in directive template

  8. 8

    Is it acceptable to use the !important directive in this case?

  9. 9

    Use parent controller required in directive

  10. 10

    AngularJS use Directive from controller

  11. 11

    Shall I use WebSocket on ports other than 80?

  12. 12

    Which datatype shall i use to store image in database(PostgreSQL)?

  13. 13

    Shall I use Visual Studio Express or Visual Studio 2013?

  14. 14

    shall I use callback pattern in my custom function? node js

  15. 15

    VB.Net - when shall I use "New" word?

  16. 16

    Which java collection shall I use to store my data?

  17. 17

    Java programming to interfaces shall I use it all the time, especially for collections

  18. 18

    Shall I extend the service class or just use an instance of it?

  19. 19

    Shall I use package name or class name to reprensent a concept?

  20. 20

    VB.Net - when shall I use "New" word?

  21. 21

    shall I use callback pattern in my custom function? node js

  22. 22

    Use CoffeeScript's class in AngularJS directive controller

  23. 23

    AngularJs - Use custom filter inside directive controller

  24. 24

    How to use/sync data of controller in a directive?

  25. 25

    Dynamically specify the controller to use in an Angular directive

  26. 26

    Use CoffeeScript's class in AngularJS directive controller

  27. 27

    how to use controller $scope in directive template

  28. 28

    How use controller variable inside a AngularJs directive

  29. 29

    How to make directive use the controller specified in directive attribute?

HotTag

Archive