简体   繁体   中英

Angular2 mdInput click focus issues

I have an Angular2 Material mdInput field, and for some reason the placeholder text doesn't go away and the input text area doesn't focus when clicking into it.

<form class="create-taskitem-form" (ngSubmit)="submitNewTask()" novalidate>
    <md-input-container class="create-task-item">
      <input mdInput
       [(ngModel)]="taskListTitle"
       (keyup.escape)="clearNewTask()"
       name="taskListTitle"
       autocomplete="off"
       [placeholder]="newItemPlaceholder"
       type="text">
    </md-input-container>
</form>

When it doesn't work, placeholder doesn't disappear and field doesn't focus but I can type into it:

在此处输入图片说明

And what's strange is that once I resize the browser, it does work and snaps into the correct focus state:

在此处输入图片说明

What's also strange is that I have a nested child component where it works just fine. Here's the root module for them:

import 'hammerjs';

@NgModule({
  imports: [
    CommonModule,
    TasksRoutingModule,
    ContentEditableModule,
    ReactiveFormsModule, 
    FormsModule, 
    MaterialModule
  ],
  exports: [
    TasksRoutingModule
  ],
  providers: [
    TasklistTypes
  ],
  declarations: [TasklistCollectionComponent, TasklistComponent, TaskItemComponent]
})
export class TasksModule { }

Try to remove the brackets around the placeholder.

...
 placeholder="New list"
...

Something wasn't registering in Angular, so I just ran a zone when a focus() or blur() event happened on the input field, and that fixed it (for now):

<md-input-container class="create-task-item">
      <input mdInput
        (blur)="onBlur()" 
        (focus)="onFocus()"
        [(ngModel)]="taskListTitle"
        (keyup.escape)="clearNewTaskList()"
        name="taskListTitle"
        autocomplete="off"
        [placeholder]="newItemPlaceholder"
        type="text">
</md-input-container>

In my component:

onBlur(){
    this.zone.run(() => {});
  }

onFocus(){
    this.zone.run(() => {});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM