Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 203
be-codified Avatar asked Jun 22 '15 16:06

be-codified


4 Answers

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.

like image 175
ryanm Avatar answered Nov 07 '22 00:11

ryanm


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);
        }
    }
})
like image 20
Dmitri Algazin Avatar answered Nov 07 '22 01:11

Dmitri Algazin


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.

like image 9
Josh Beam Avatar answered Nov 07 '22 00:11

Josh Beam


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

like image 1
Lalit Sachdeva Avatar answered Nov 06 '22 23:11

Lalit Sachdeva