angularjs - Angular Material 对话框 : Set focus on the first item

标签 angularjs dialog focus angular-material

我使用 Angular 1.5.x 和 Material Design,并且有一个自定义对话框,我想在显示对话框时选择第一个输入字段。

我该怎么做?

我整理了一个问题的实例: https://codepen.io/anon/pen/xdoqmZ?editors=1010

这是代码;

  $mdDialog.show({
    parent: parentEl,
    targetEvent: $event, // determines source location of the dialog
    template:
            ' <md-dialog>' +
        '   <md-content>Hello {{ username }}!</md-content>' +
        '   <div class="md-actions">' +
        '     <form>' + 
        '       <div>Given names: <input /></div>' +
        '       <div>Surname:  <input /></div>' +
        '       <div>Address:  <input /></div>' +
        '       <div>Building: <input /></div>' +
        '       <div>Level:    <input /></div>' +
        '       <div>Street:   <input /></div>' +
        '       <div>Suburb:   <input /></div>' +
        '       <div>Postcode: <input /></div>' +
        '       <div>Country:  <input /></div>' +
        '       <div>State:    <input /></div>' +
        '       <div>ABN:      <input /></div>' +
        '       <div>' +
        '         <md-button ng-click="closeDialog()">' +
        '           Close Greeting' +
        '         </md-button>' +
        '       </div>' +
        '     </form>' +
        '   </div>' +
        ' </md-dialog>',
    onComplete: onCompleteAnimation,
    locals: {
      name: $scope.username
    },
    controller: DialogController
  });

在此示例中,我希望出现的对话框中关注焦点的是名字。

最佳答案

在 onCompleteAnimation 函数中找到第一个输入并获得焦点

    onComplete: function(s,e) {
        $(e).find('input').first().focus()
    },
    locals: {
      name: $scope.username
    },
    controller: DialogController

https://codepen.io/anon/pen/ZKgWjQ?editors=1010

关于angularjs - Angular Material 对话框 : Set focus on the first item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44277332/

相关文章:

javascript - Angular 1.5 组件与旧指令 - 链接函数在哪里?

android - 将图像从 ImageView 设置为 Dialog

wpf - 为什么 PredictFocus() 不支持 FocusNavigationDirection.Next?

javascript - a4j :support is used 时焦点消失

javascript - 使用 $emit 和 $on 单击按钮时显示 div 标签

javascript - 在另一个指令的模板上使用 Angular Directive(指令)?

javascript - $rootScope 和 $scope,共享对象

java - 访问子级的数据并在父级测试类中使用它。

cordova - 从phonegap应用程序打开电话网络设置对话框

css - 将 CSS 样式添加到除焦点文本区域之外的所有内容