How to bind boolean values in angular directives?

You can configure your own 1-way databinding behavior for booleans like this:

link: function(scope, element, attrs) {

    attrs.$observe('collapsable', function() {

        scope.collapsable = scope.$eval(attrs.collabsable);
    });

}

Using $observe here means that your “watch” is only affected by the attribute changing and won’t be affected if you were to directly change the $scope.collapsable inside of your directive.

Leave a Comment