gpt4 book ai didi

angularjs - 路由更改后无法专注于 Angular 表单文本输入

转载 作者:行者123 更新时间:2023-12-05 03:11:16 24 4
gpt4 key购买 nike

我正在使用 Angular 和 Angular Messages 创建一个表单。这个表单位于一个模板中,该模板通过 Angular Route 进入 View 。当我第一次加载表单时,一切正常。然后,当我加载不同的 View 并切换回表单的 View 时,我无法专注于文本输入。发生了什么事?

HTML

<form name='submission' ng-submit='submit()'>

<label class='text-input-group' for='name'>
<div class='label'>Name</div>
<input id='name' name='name' ng-model='submissionName' type='text' required>
<div ng-messages='submission.name.$error' ng-if='submission.name.$touched'>
<div ng-message='required'>* Please enter your name</div>
</div>
</label>

<label class='text-input-group' for='email'>
<div class='label'>Email</div>
<input id='email' name='email' ng-model='submissionEmail' type='email' required>
<div ng-messages='submission.email.$error' ng-if='submission.email.$touched'>
<div ng-message='required'>* Please enter your email address</div>
<div ng-message='email'>* Please enter a valid email address</div>
</div>
</label>

<label class='text-input-group' for='message'>
<div class='label'>Message</div>
<textarea id='message' name='message' ng-model='submissionMessage' ng-maxlength='2000' maxlength='2000' required></textarea>
<div ng-messages='submission.message.$error' ng-if='submission.message.$touched'>
<div ng-message='required'>* No message?</div>
<div ng-message='maxlength'>* Your message unfortunately can&#x27;t exceed 20,000 characters</div>
</div>
</label>

<label class='checkbox-input-group' for='send-user-a-copy'>
<div class='label'>Send me a copy</div>
<input id='send-user-a-copy' name='sendUserACopy' ng-init='submissionSendUserACopy = false;' ng-model='submissionSendUserACopy' type='checkbox'>
</label>

<button type='submit'>Button</button>

</form>

JavaScript

var contact = angular.module('app.contact', ['ngRoute']);

contact.config(['$routeProvider', function($routeProvider) {

$routeProvider.when('/contact', {
templateUrl: 'partials/contact.html',
controller: 'ContactController'
});

}]);

contact.controller('ContactController', ['$scope', function($scope) {

$scope.reset = function() {
$scope.submissionName = '';
$scope.submissionEmail = '';
$scope.submissionMessage = '';
$scope.submissionSendUserACopy = '';
$scope.submission.$setPristine();
}

$scope.$on('$routeChangeSuccess', function() {
console.log($scope.submission);
$scope.reset();
});

$scope.submit = function() {

if($scope.submission.$valid) {
console.log({
'name' : $scope.submissionName,
'email' : $scope.submissionEmail,
'message' : $scope.submissionMessage,
'sendUserACopy' : $scope.submissionSendUserACopy
});
}

}

}]);

任何答案/建议将不胜感激。谢谢!

最佳答案

我写这篇文章是为了解决焦点不应用于 Angular 中的路由更改的问题。

import { Directive, ElementRef, AfterContentInit } from '@angular/core';

@Directive({
selector: '[focusOnInit]'
})
export class FocusDirective implements AfterContentInit {

constructor(public el: ElementRef) {}

ngAfterContentInit() {
this.el.nativeElement.focus();
}
}

用法

<input type="text" focusOnInit>

关于angularjs - 路由更改后无法专注于 Angular 表单文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37617839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com