In Angular 1.5, how to bind an attribute component as boolean?
In angular 1.5 onwards you can use <
& @
for one way binding. The main differnece between these two is <
has ability to pass an object with its original data type to component.
isactive: '<'
Just use a one-way binding instead of a string binding:
angular.module('app') .component('appMenuitem', { transclude: false, controller: menuitemController, bindings: { label: '@', isactive: '<' }, templateUrl: 'angular/components/simple/menuitem/menuitem.html' });
<
forces you to use true
and false
for your attribute values, which is not entirely HTML-like. For example, we often write:
<input type="text" disabled>
instead of
<input type="text" disabled="disabled">
To continue doing this with your AngularJS components, you can use a @
binding with parse-string-boolean (or similar) in $onChanges
:
bindings: { paramSomething: '@something'}
,
function $onChanges(changes) { if (changes.paramSomething) { switch (typeof this.paramSomething) { case 'string': { this.isSomething = parseBoolean(this.paramSomething, true); break; } case 'undefined': { this.isSomething = false; break; } } }
,
<my-component something></my-component>