How to set the default value of directive attribute?

user70192

I am building a custom element directing in AngularJS. My element will be used like this:

<my-element my-attribute="false"></my-element>

If the user does NOT set my-attribute, I want it to default to true. How do I give the directive attribute a default value? Currently, I have the following:

myApp.directive('myElement', function ($window) {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      myAttribute: '='
    },
    controller: function($scope) {

    },
    template: '<div>Hello <span ng-if="myAttribute == true">, World</span></div>'
  };
});

Thank you!

Blackhole

Make the attribute optional

First, be sure to make the attribute optional. With your current code, if your directive is used like that…

<!-- Notice the absence of attribute -->
<my-element></my-element> 

… then AngularJS will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception as soon as you'll try to set a value to $scope.myAttribute. So, as said in the documentation, add an interrogation point in your scope definition:

scope: {
    myAttribute: '=?',
},

Define the default value

Then, you can simply set the default value in your controller:

controller: function ($scope) {
    if (angular.isUndefined($scope.myAttribute)) {
        $scope.myAttribute = true;
    }
},

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set a default attribute value for a Laravel / Eloquent model?

From Dev

AngularJs: How to get the attribute value of a attribute type directive

From Dev

How to get default product attribute set id?

From Dev

Set attribute value of angular directive from another controller

From Dev

Default Content Type Attribute value for Page Directive

From Dev

Java: how to set default value to annotation with another annotation as its attribute?

From Dev

How to get and run an attribute of value "{{aaa}}/{{bbb}}" in a directive?

From Dev

Install4j - how to set default value for "Can be executed multiple times" attribute?

From Dev

How to set a default value for attribute if it doesn't exist in the XDocument object

From Dev

Is there a way to set a default value for a chef resource attribute?

From Dev

Angular attribute directive value

From Dev

VertexArrayObject, how can i set "default" attribute value?

From Dev

How to get attribute value in template in angularjs custom directive?

From Dev

How to set a default value in an AngularJS Directive Scope?

From Dev

Angular element directive how to get attribute value

From Dev

How to set an attribute with a blank value?

From Dev

AngularJS: How to set an attribute to a custom directive

From Dev

Set href in attribute directive in Angular

From Dev

AngularJs: How to get the attribute value of a attribute type directive

From Dev

In Angular how can I set a watch inside my directive that fires when the value of an attribute changes?

From Dev

How to set a value to an attribute with Javascript

From Dev

How do I set default value of select box in directive

From Dev

How to set a default value for attribute if it doesn't exist in the XDocument object

From Dev

How to set an attribute with a blank value?

From Dev

How to set initial value in directive property

From Dev

How to set a default value for const?

From Dev

Angular js directive : How to set selected value or default value

From Dev

Set string as directive attribute value that maps to a member on the controller scope

From Dev

Get default value if attribute not set or faulty in Javascript

Related Related

  1. 1

    How to set a default attribute value for a Laravel / Eloquent model?

  2. 2

    AngularJs: How to get the attribute value of a attribute type directive

  3. 3

    How to get default product attribute set id?

  4. 4

    Set attribute value of angular directive from another controller

  5. 5

    Default Content Type Attribute value for Page Directive

  6. 6

    Java: how to set default value to annotation with another annotation as its attribute?

  7. 7

    How to get and run an attribute of value "{{aaa}}/{{bbb}}" in a directive?

  8. 8

    Install4j - how to set default value for "Can be executed multiple times" attribute?

  9. 9

    How to set a default value for attribute if it doesn't exist in the XDocument object

  10. 10

    Is there a way to set a default value for a chef resource attribute?

  11. 11

    Angular attribute directive value

  12. 12

    VertexArrayObject, how can i set "default" attribute value?

  13. 13

    How to get attribute value in template in angularjs custom directive?

  14. 14

    How to set a default value in an AngularJS Directive Scope?

  15. 15

    Angular element directive how to get attribute value

  16. 16

    How to set an attribute with a blank value?

  17. 17

    AngularJS: How to set an attribute to a custom directive

  18. 18

    Set href in attribute directive in Angular

  19. 19

    AngularJs: How to get the attribute value of a attribute type directive

  20. 20

    In Angular how can I set a watch inside my directive that fires when the value of an attribute changes?

  21. 21

    How to set a value to an attribute with Javascript

  22. 22

    How do I set default value of select box in directive

  23. 23

    How to set a default value for attribute if it doesn't exist in the XDocument object

  24. 24

    How to set an attribute with a blank value?

  25. 25

    How to set initial value in directive property

  26. 26

    How to set a default value for const?

  27. 27

    Angular js directive : How to set selected value or default value

  28. 28

    Set string as directive attribute value that maps to a member on the controller scope

  29. 29

    Get default value if attribute not set or faulty in Javascript

HotTag

Archive