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

kob490

In AngularJS, I have the following scenario where a directive can accept an optional boolean parameter which should default to true by default, whenever it is not specified.

Example:

<my-directive data-allow-something="false">
    ... this works as expected as no default value should be set in this case ...
</my-directive>

<my-directive>
    ... but in this case i'd like allowSomething to equal true  ...
</my-directive>

I've tried the following approach, but for some reason the true value doesn't stick on allowSomething. making it a '=?' optional two way binding doesn't work neither as my passed value should be a concrete true/false and not a field reference.

angular.module('myApp').directive('myDirective') {
    ...
    controller: function($scope){
        if (!$scope.allowSomething)
            $scope.allowSomething = true;
    },
    ....
    scope: function(){
        allowSomething: '@'
    }
    ...
}

I'm sure there should be a simple way to achieve this, so what am i missing?

The solutions given at the following ticket: AngularJS directive with default options are not sufficient for my needs since the $compile function will prevent the link function from working. plus, the passed-in boolean value is not a reference type and i cannot give it a two-way binding.

Michael Sheely

I think a better way to check that value is look for an undefined value like this:

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

I had the same issue once and this worked for me. I think the best method is to use angular's methods for handling things.

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 default value for scope variable in angularjs

From Dev

how to bind directive scope value to template in angularjs

From Dev

What is the default for AngularJS directive scope?

From Dev

AngularJS : How to change scope value from isolated scope within directive

From Dev

How to set the default value of directive attribute?

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJs: Directive with function for template property. How to get scope value?

From Dev

How to pass value back from angularjs directive scope with javascript event

From Dev

How to pass value back from angularjs directive scope with javascript event

From Dev

AngularJs: Directive with function for template property. How to get scope value?

From Dev

How to set scope variable value in angularjs template

From Dev

How to set scope variable value in angularjs template

From Dev

AngularJS directive with isolated scope - default attributes values

From Dev

AngularJS directive with isolated scope - default attributes values

From Dev

AngularJS How: Scope hierarchy for Directive

From Dev

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

From Dev

how to set dropdown default value using angularjs

From Dev

Angularjs how to set the default value for select

From Dev

How to set default decimal value to input in angularjs

From Dev

How to set default value for select in angularjs

From Dev

AngularJS Directive - Restrict: 'A' and set that directive equal to a value

From Dev

How do I set default value of select box in directive

From Dev

Change controller scope value from directive AngularJS

From Dev

angularjs directive + apply scope value in many controller

From Dev

angularjs controller access directive scope value

From Java

How to Unit Test Isolated Scope Directive in AngularJS

From Dev

How to set validity in directive (angularjs)

From Dev

AngularJS / CSS - How to set the width of a select box to a value from $scope?

Related Related

  1. 1

    How to set default value for scope variable in angularjs

  2. 2

    how to bind directive scope value to template in angularjs

  3. 3

    What is the default for AngularJS directive scope?

  4. 4

    AngularJS : How to change scope value from isolated scope within directive

  5. 5

    How to set the default value of directive attribute?

  6. 6

    AngularJs directive, scope with value and function

  7. 7

    AngularJs directive, scope with value and function

  8. 8

    AngularJs: Directive with function for template property. How to get scope value?

  9. 9

    How to pass value back from angularjs directive scope with javascript event

  10. 10

    How to pass value back from angularjs directive scope with javascript event

  11. 11

    AngularJs: Directive with function for template property. How to get scope value?

  12. 12

    How to set scope variable value in angularjs template

  13. 13

    How to set scope variable value in angularjs template

  14. 14

    AngularJS directive with isolated scope - default attributes values

  15. 15

    AngularJS directive with isolated scope - default attributes values

  16. 16

    AngularJS How: Scope hierarchy for Directive

  17. 17

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

  18. 18

    how to set dropdown default value using angularjs

  19. 19

    Angularjs how to set the default value for select

  20. 20

    How to set default decimal value to input in angularjs

  21. 21

    How to set default value for select in angularjs

  22. 22

    AngularJS Directive - Restrict: 'A' and set that directive equal to a value

  23. 23

    How do I set default value of select box in directive

  24. 24

    Change controller scope value from directive AngularJS

  25. 25

    angularjs directive + apply scope value in many controller

  26. 26

    angularjs controller access directive scope value

  27. 27

    How to Unit Test Isolated Scope Directive in AngularJS

  28. 28

    How to set validity in directive (angularjs)

  29. 29

    AngularJS / CSS - How to set the width of a select box to a value from $scope?

HotTag

Archive