1

I would like to set a boolean property on an angular2 component.

Given the following (that is incorrect) ...

.dart

@Component
....
@Input()
bool hasColour = false;

.html

...
<span [class.blue-colour] = 'hasColour'></span>
...

<colour-component hasColour></colour-component>

... how can I fix the above to be able to use hasColour to bind a class?

Thanks

1 Answer 1

2

Bind to the hasColour input property:

<colour-component [hasColour]="true"></colour-component>
Sign up to request clarification or add additional context in comments.

3 Comments

I thought the presence of the property was enough to say it is true, like the required property. Why don't I have to put [required] = 'true', but just required?
When you bind to an Input property, you need to use the proper binding convention ([property]="value"). If you are specifying an attribute directive (like required), then the binding convention is not necessary.
Thanks for the explanation. The solution does work too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.