最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to focus and navigate li elements in Angular 4? - Stack Overflow

programmeradmin1浏览0评论

I am trying to make a custom select option in Angular 4. But struck up unsure how to select the li options when focused in the input element.

Here's my html code:

<div class="input-container">
     <input type="text" id="iLocation"
               (focus)="onLocationFocus()"
               (blur)="onLocationBlur()">
     <span class="underline"></span>
     <label for="iLocation">Preferred Type</label>

     <div class="options-container" tabindex="0" #optionsContainerRef>
        <ul>
           <li *ngFor="let loc of locationOptions">{{loc.option}}</li>
        </ul>
     </div>
</div>

and here's my ponent typescript:

locationOptions = [{icon: 'school', option: 'School'}, {icon: 'home', option: 'Home'}];
@ViewChild("optionsContainerRef") optionsContainerRef: ElementRef;
constructor(private renderer: Renderer2) { }
    onLocationFocus() {
      this.renderer.setAttribute(this.optionsContainerRef.nativeElement.children[0].children[0], 'focus', 'true');
    }
    onLocationBlur() {}

Even though, the renderer adds the focus="true" element, the li element is not getting focused. I have written css code li background-color when focused, but the element isn't getting focused and the color isn't changing.

The other issue I am struck is like how to navigate through the li elements using keyboard up and down arrows. What would be the effective way to do it in angular 4? I have seen this reference, but unsure how would I do it in angular.

Please guide and help me out with this issues.

I am trying to make a custom select option in Angular 4. But struck up unsure how to select the li options when focused in the input element.

Here's my html code:

<div class="input-container">
     <input type="text" id="iLocation"
               (focus)="onLocationFocus()"
               (blur)="onLocationBlur()">
     <span class="underline"></span>
     <label for="iLocation">Preferred Type</label>

     <div class="options-container" tabindex="0" #optionsContainerRef>
        <ul>
           <li *ngFor="let loc of locationOptions">{{loc.option}}</li>
        </ul>
     </div>
</div>

and here's my ponent typescript:

locationOptions = [{icon: 'school', option: 'School'}, {icon: 'home', option: 'Home'}];
@ViewChild("optionsContainerRef") optionsContainerRef: ElementRef;
constructor(private renderer: Renderer2) { }
    onLocationFocus() {
      this.renderer.setAttribute(this.optionsContainerRef.nativeElement.children[0].children[0], 'focus', 'true');
    }
    onLocationBlur() {}

Even though, the renderer adds the focus="true" element, the li element is not getting focused. I have written css code li background-color when focused, but the element isn't getting focused and the color isn't changing.

The other issue I am struck is like how to navigate through the li elements using keyboard up and down arrows. What would be the effective way to do it in angular 4? I have seen this reference, but unsure how would I do it in angular.

Please guide and help me out with this issues.

Share Improve this question asked Jul 29, 2017 at 8:20 starlightstarlight 7854 gold badges16 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Rather than using focus, use ngClass instead - like this:

in your ponent class:

  locationOptions = [{ icon: 'school', option: 'School' }, { icon: 'home', option: 'Home' }];
  public focusElement: number = -1;
  constructor() { }
  onLocationFocus() {
    this.focusElement = 0;
  }
  onLocationBlur() {
    this.focusElement = -1;
  }

  onArrowUp() {
    if (this.focusElement > 0) {
      this.focusElement--;
    }
  }

  onArrowDown() {
    if (this.focusElement <= this.locationOptions.length - 2) {
      this.focusElement++;
    }
  }

Then the template looks like this:

<input type="text" id="iLocation"
               (focus)="onLocationFocus()"
               (blur)="onLocationBlur()"
               (keydown.ArrowUp)="onArrowUp()"
               (keydown.ArrowDown)="onArrowDown()">
     <span class="underline"></span>
     <label for="iLocation">Preferred Type</label>

     <div class="options-container">
        <ul>
            <li #item [ngClass]="{'focus-background': focusElement == item.tabIndex}"
              *ngFor="let loc of locationOptions; let i = index" tabIndex="{{i}}">
             {{loc.option}} </li>
        </ul>
     </div>

and a little CSS like this

.focus-background {
  background-color: lightblue;
}
发布评论

评论列表(0)

  1. 暂无评论