I have a custom angular directive that graphically represents an “activity” in my webapp. I use it like this:
<activity-box ng-repeat="act in activities"
model="act" active="{{currentActivity == act}}" />
My directive has an isolated scope and declares model and active like this:
appDirectives.directive('activityBox', function() {
return {
template: '<div ng-class="{activityActive: active == \'true\'}">{{model.name}}</div>',
restrict: 'E',
replace: true,
scope: {
model: '=',
active: '@'
},
link: ...
};
});
I have no worry about the model attribute, but my active attribute is always treated as a string. When currentActivity == act is true, then active holds the string value "true" (and not the boolean true), or else, "false" (and not false).
This means that although it conceptually is a boolean, I must treat it as a string. For instance, I'd like to write ng-class="{activityActive: active}" instead ng-class="{activityActive: active == 'true'}". Right now, if I forget the extra part, this always evaluate to true, as both "false" and "true" are truthy.
Is there any way for me to obtain non-string attributes like this? What's the best way to achieve this?
@it is always treated as a string (in your case it is interpolated first though). If you want the value of an expression you should go with=. PLNKR that demonstrates it.'='.