be-codified
be-codified

Reputation: 6124

AngularJS: passing boolean value to directive

I can not pass boolean value to my directive.

Here is my HMTL:

<city-zip city="clientCity" zip="clientZip" requiredParam="'true'"></city-zip>

And directive:

.directive('cityZip', function() {
    return {
        restrict: 'E',
        templateUrl: '../templates/templateCityZip.html',
        scope: {
            city: '=',
            zip: '='
        },
        controller: function($scope) {}
    }
});

Any help would be much appreciated.

Upvotes: 14

Views: 28874

Answers (4)

Lalit Sachdeva
Lalit Sachdeva

Reputation: 6619

There are 3 parameters that you can pass in the link function which work on the directive. Parameters are scope, element and attributes.

  1. scope gives the scope of the controller that directive is put under.

  2. element passes the information about the DOM element on which it is applied

  3. attributes passes the information about all DOM element attributes that are on the element.

    <city-zip ng-app="myapp" city="clientCity" zip="clientZip" required-param="true"></city-zip>
    
    angular.module("myapp", []).directive('cityZip', function() {
    return {
        restrict: 'E',
        templateUrl: '',
        scope: {
            requiredParam:'@'
        },
        link: function(scope, $el, attrs) {
            alert( attrs.requiredParam);
        }
    }
    

    })

Working jsFiddle

Upvotes: 1

ryanm
ryanm

Reputation: 2268

I think the simplest / cleanest answer has not yet been included for this question. This answer also fits within the HTML5 Spec for a boolean attribute - http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes

2.5.2 Boolean attributes

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

HTML:

<city-zip city="clientCity" zip="clientZip" requiredParam></city-zip>

And the directive:

.directive('cityZip', function() {
    return {
        restrict: 'E',
        templateUrl: '../templates/templateCityZip.html',
        scope: {
            city: '=',
            zip: '='
        },
        controller: function($scope, $attrs) {
            $scope.requiredParamExists = $attrs.hasOwnProperty( 'requiredParam' );
        }
    }
});

Simple, fits cleanly with HTML5 spec for boolean attributes, and no need to coerce a string to a scope variable ('requiredParam': '=').

Note in the example code above, if minified, the required variables $scope and $attrs will change to a shorter string and break the code, but that's another issue.

Upvotes: 10

Dmitri Algazin
Dmitri Algazin

Reputation: 3456

HTML

<city-zip city="clientCity" zip="clientZip" required-param="true"></city-zip>
<city-zip city="clientCity" zip="clientZip" required-param="{{ someBooleanValue }}"></city-zip>

Angular

.directive('cityZip', function() {
    return {
        restrict: 'E',
        templateUrl: '../templates/templateCityZip.html',
        scope: {
            city: '=',
            zip: '=',
            requiredParam:'@'
        },
        link: function(scope) {
            console.log("requiredParam", scope.requiredParam);
        }
    }
})

Upvotes: 13

Josh Beam
Josh Beam

Reputation: 19772

Inside link, you can access the attribute:

return {
    // code
    link: link
}

function link(scope, $el, attrs) {
    var requiredParam = attrs.requiredParam === 'true';
}

That'll coerce the string value to a boolean (if the string value is 'true', it'll return true, otherwise it'll return false.)

The main part here is how to convert a string value "true" or "false" to its boolean form, since !!'true' and !!'false' both return true. See this answer for the solution and extended discussion.

If you need to use the value in your controller, you can do the same pattern in the scope object and pass it in its coerced form to the coupled controller.

Upvotes: 9

Related Questions