I am learning angular2. Here I have the following Problem:
I have a select
field which should work as a boolean value:
<select [(ngModel)]="caseSensitive"> <option>false</option> <option>true</option> </select>
No if i use it in my Filter it will send as a string not as a boolean. Is it possible to convert it using a converter or something like that:
Here my complete HTML code:
<input [(ngModel)]="nameFilter"/> <select [(ngModel)]="caseSensitive"> <option>false</option> <option>true</option> </select> <table> <tr *ngFor="let p of (persons | MyFilter: nameFilter:caseSensitive); let i = index"> <td>{{i + 1 }} </td> <td>{{ p.givenName+" "+ p.familyName }}</td> <td><img src="/img/flags/{{ p.nationality}}.png"></td> </tr> </table>
The ts code:
import { Component } from '@angular/core'; import {MyFilter} from './MyFilter'; @Component({ selector: 'pizza-root', pipes: [MyFilter], templateUrl: 'app.component.html' }) export class AppComponent { public year = new Date().getFullYear(); public persons =[{"givenName":"Paul", "familyName": "Smith", "nationality":"american"}, {"givenName":"Jens", "familyName":"myName1", "nationality":"german"}, {"givenName":"Ernst", "familyName":"myName1", "nationality":"german"}, {"givenName":"Jenny", "familyName":"myName1", "nationality":"german"}]; constructor (){ console.log(this.persons); } }
The Pipe:
import { Pipe, PipeTransform} from '@angular/core'; @Pipe({ name: 'MyFilter' }) export class MyFilter implements PipeTransform{ transform( items: any[], args: string, caseSensitive : boolean ):any { if (items != null && args != undefined && args != ''){ if (caseSensitive){ console.log("caseSensitive") return items.filter(item=>item.givenName.indexOf(args)!== -1); } else { console.log("caseInSensitive") return items.filter(item=> item.givenName.toLowerCase().indexOf(args.toLowerCase())!== -1); } } console.log("else") return items; } }
The Problem is, that the pipe not working correctly because caseSensitive
is the bind as a string not as a boolean value.
To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.
Use the Vue way, set an array of options in your Vue data then use v-for to render all the options from the array. Each option should have 2 properties: a text and a value. The value should be the boolean you are looking for. Now whenever the user changes the selected option, it will always be boolean.
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
We can convert boolean to String in java using String. valueOf(boolean) method. Alternatively, we can use Boolean. toString(boolean) method which also converts boolean into String.
The values in <option>
are strings or they are stringified if other types are provided. If you use ngValue
instead, you can use other types and ngModel
retains the type.
<select [(ngModel)]="caseSensitive"> <option [ngValue]="false">false</option> <option [ngValue]="true">true</option> </select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With