javascript - Angular 2 : Toggle Boolean based on a condition - Stack Overflow

I have a search-bar and I want to show all the filtered names when the input value length is 2 or longe

I have a search-bar and I want to show all the filtered names when the input value length is 2 or longer.

I managed to get the value.length out of the input field. Now I'm stuck. At first I had done:

if (value.length >= 2){
  showNames: true;
} 

Default showNames is false. And when the length was 2 or higher, it set to true. So, that's working. Only when the user erases the text, so the value.length is below 2 again, the boolean won't turn false again. I tried if else, but I know that's not correct in Angular 2.

<ion-searchbar (ionInput)="getNames($event)"></ion-searchbar>
<ion-list *ngIf="showNames">
   <ion-item *ngFor="let name of names">
     {{ name }}
   </ion-item>
</ion-list>

I know I can toggle a boolean from a button click, but I just want to toggle it from the length of a value.

*ngIf="value.length >= 2" doesn't work either, because I create the variable 'value' in my Typescript. So in my HTML it's not defined. And I don't want to use the big formula to calculate the length, that's why I created a variable.

How can I either

  1. Pass the variable 'value' from my Typescript to my HTML so I can use the *ngIf="value.length > 2"?
  2. OR do some kind of If / Else in my Typescript, so I can use a single boolean like *ngIf="showNames" in my HTML?

I have a search-bar and I want to show all the filtered names when the input value length is 2 or longer.

I managed to get the value.length out of the input field. Now I'm stuck. At first I had done:

if (value.length >= 2){
  showNames: true;
} 

Default showNames is false. And when the length was 2 or higher, it set to true. So, that's working. Only when the user erases the text, so the value.length is below 2 again, the boolean won't turn false again. I tried if else, but I know that's not correct in Angular 2.

<ion-searchbar (ionInput)="getNames($event)"></ion-searchbar>
<ion-list *ngIf="showNames">
   <ion-item *ngFor="let name of names">
     {{ name }}
   </ion-item>
</ion-list>

I know I can toggle a boolean from a button click, but I just want to toggle it from the length of a value.

*ngIf="value.length >= 2" doesn't work either, because I create the variable 'value' in my Typescript. So in my HTML it's not defined. And I don't want to use the big formula to calculate the length, that's why I created a variable.

How can I either

  1. Pass the variable 'value' from my Typescript to my HTML so I can use the *ngIf="value.length > 2"?
  2. OR do some kind of If / Else in my Typescript, so I can use a single boolean like *ngIf="showNames" in my HTML?
Share Improve this question edited Jul 6, 2016 at 15:07 UrsinusTheStrong 1,2071 gold badge16 silver badges33 bronze badges asked Jul 6, 2016 at 14:18 J.DDJ.DD 3882 silver badges15 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

You can bind to the model of the searchbar like this:

<ion-searchbar [(ngModel)]="searchValue" (ionInput)="getNames($event)"></ion-searchbar>
<ion-list *ngIf="searchValue.length >= 2">
   <ion-item *ngFor="let name of names">
     {{ name }}
   </ion-item>
</ion-list>

And in your ponent define such a variable:

export class ComponentA {
    searchValue: string = "";

    // ...
}

You could make showNames a function and aim to do *ngIf="showNames()", or if it is that short perform the check right there: *ngIf="value.length >= 2"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341346a4623340.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信