Angular 2 condition inside href
I have this line of code
<a href="{{banner.url}}"></a>
But sometimes url is null. How can I check it for null and then pass the url to the href?
I tried this
<a href="{{banner.url === null ? javascript:void(0) : banner.url}}"></a>
But this throws an error
zone.js:388 Unhandled Promise rejection: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 0 in [{{banner.url === null ? javascript:void(0) : banner.url}}]
+3
source to share
2 answers
<a [href]="banner.url === null ? 'javascript:void(0)' | safeUrl : banner.url"
If safeUrl
is a channel as shown at fooobar.com/questions/12313 / ...
otherwise javascript:void(0)
it probably won't be added for security reasons (haven't tried it myself)
+4
source to share