Angular2使用* ngIf和焦点指令?

前端之家收集整理的这篇文章主要介绍了Angular2使用* ngIf和焦点指令?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我尝试放置一个按钮,显示/隐藏带有布尔组件属性的输入字段.如果按钮显示输入,则应在输入上设置焦点.但它似乎不起作用.如果我删除* ngIf,焦点指令工作正常.

我创建了一个显示我的意思的plunker.描述我的“问题”有点困难.

组件中的html

  1. <input *ngIf="filterShow.options"
  2. [focus]="filterFocus.options"
  3. [(ngModel)]="filter.options">
  4.  
  5. <button type="button"
  6. (click)="setShowFilter('options')">
  7. focus
  8. </button>

setShowFilter()函数

  1. private setShowFilter(filter: string) {
  2. this.filterShow[filter] = !this.filterShow[filter];
  3.  
  4. /* reset filter */
  5. this.filter[filter] = "";
  6.  
  7. this.filterFocus[filter].emit(true);
  8. }

focus.directive.ts

  1. @Directive({
  2. selector: '[focus]'
  3. })
  4. export class FocusDirective implements OnInit {
  5.  
  6. @Input('focus') focusEvent: EventEmitter<boolean>;
  7.  
  8. constructor(private elementRef : ElementRef,private renderer : Renderer ) { }
  9.  
  10. ngOnInit() {
  11. this.focusEvent.subscribe(event => {
  12. this.renderer
  13. .invokeElementMethod(this.elementRef.nativeElement,'focus',[]);
  14. });
  15. }
  16. }
EventEmitters用于输出,不用于输入.尝试这样的事情:
  1. @Directive({
  2. selector: '[focus]'
  3. })
  4. export class FocusDirective implements OnChanges {
  5.  
  6. @Input('focus') focus: boolean;
  7.  
  8. constructor(private elementRef : ElementRef,private renderer : Renderer ) { }
  9.  
  10. ngOnChanges() {
  11. if (this.focus) {
  12. this.renderer
  13. .invokeElementMethod(this.elementRef.nativeElement,[]);
  14. }
  15. }
  16. }

猜你在找的Angularjs相关文章