gpt4 book ai didi

javascript - 如何在 Angular 1.5 中重置表单

转载 作者:行者123 更新时间:2023-12-03 02:28:09 24 4
gpt4 key购买 nike

我有这个标记

如何在相应的组件中重置它?

<form name="voiceForm" novalidate>...</form>
<小时/>

我已经尝试过:

this.voiceForm.$setPristine();
self.voiceForm.reset();

但出现错误 voiceForm 未定义。

这是我的组件:

(function (app) {
app.component('voiceFormComponent', {
templateUrl: 'partials/voice-form.html',
controller: ['$scope', '$state', '$stateParams',
function ($scope, $state, $stateParams) {

var self = this;

console.log("in voice prompt component");

self.addVoice = function () {
self.voiceForm.$setPristine();
$state.go("add");
}

最佳答案

您可以传递表单并调用setPristine

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

app.controller('MainCtrl', function() {
this.data = {
name: ''
};

this.reset = function(form) {
this.data.name = '';
form.$setPristine();
};

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="formReset">

<head>
<title>form.$submitted</title>
<script src="http://code.angularjs.org/1.3.2/angular.min.js"></script>
<script src="app.js"></script>
</head>

<body>
<div ng-controller="MainCtrl as ctrl">
<form name="form" novalidate>
<input name="name" ng-model="ctrl.data.name" placeholder="Name" required />
<input type="submit" />
<button type="button" class="button" ng-click="ctrl.reset(form)">Reset</button>
</form>

<pre>

Submitted: {{form.$submitted}}

</pre>
</div>

关于javascript - 如何在 Angular 1.5 中重置表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850843/

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