I need to not pass in any query parameters in the URL if the value not defined. In AngularJS when some sort of parameters were passed into $resource
service and its value was empty or not defined, then it was never appended to the URL string, however in Angular the Http service along with its RequestOptions object which is used to set headers and parameters behaves differently.
For example this input element:
<select class="form-control form-control-sm" formControlName="catalogCode" title="Catalog Code">
<option value="">- Catalog Code -</option>
<option *ngFor="let option of codes" [value]="option.code">
{{ option.text }}
</option>
</select>
This updates the QueryParameters
I have defined and it is used in service like this:
getOfferings(parameters: QueryParameters): Observable<Offering[]> {
let requestOptions = new RequestOptions({
search: {
catalogCode: parameters.catalogCode
}
});
return this.http.get(`${environment.BASE_URL}/offerings`, requestOptions)
.map(response => response.json());
};
If the parameters.catalogCode
is populated, query runs fine and the URL looks proper: http://localhost:4200/offerings?catalogCode=CODE1
, however when I select the empty option from the list, the URL will be ?catalogCode=
with empty value.
My backend unfortunately does not like the empty catalogCode
so I would really like to prevent that as it used to be in old Angular. Is there any proper way of clearing it?