Angular query params as boolean type, not string
Map the value to a boolean by performing a string comparison against 'true'
.
ngOnInit() {
this.paywallQuery$ = this.route.queryParamMap.pipe(
map(params => {
const value = params.get('paywall');
return value ? value.toLocaleLowerCase() === 'true' : false;
})
);
}
If I already depend on Angular's CDK I would use the boolean coercion function (coerceBooleanProperty
) to convert that string to a proper boolean.
It's pretty bare bones, but this is the thing with urls and attributes in the web component space vs using javascript to do the binding.
queryparams
, pathparams
, and attributes
are all string values so you gotta convert them for them to make sense in your implementation.
This would be my code:
import { coerceBooleanProperty } from '@angular/cdk/coercion';
this.paywallQuery = coerceBooleanProperty(this.route.snapshot.paramMap.get('paywall'))
and you can see some examples here: https://github.com/angular/components/blob/master/src/cdk/coercion/boolean-property.spec.ts