Javascript 如何在angular 1.5中重置窗体
我有这个标记 如何在相应的组件中重置它Javascript 如何在angular 1.5中重置窗体,javascript,angularjs,forms,Javascript,Angularjs,Forms,我有这个标记 如何在相应的组件中重置它 <form name="voiceForm" novalidate>...</form> 但有一个错误,未定义voiceForm 这是我的组件: (function (app) { app.component('voiceFormComponent', { templateUrl: 'partials/voice-form.html', controller: ['$scope', '$st
<form name="voiceForm" novalidate>...</form>
但有一个错误,未定义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=角度。模块“formReset”,[]; 应用程序控制器'MainCtrl',函数{ 此参数。数据={ 姓名: }; this.reset=functionform{ this.data.name=; 形式:$setPristine; }; }; 表格:$已提交 重置
我有一个类似的问题,当我在一个表单中有一个不允许的表单时,修复方法是使用ng表单作为子表单。可能尝试使用ng表单?您是使用控制器作为语法还是控制器?是否尝试使用重置按钮?@Sajeetharan我使用组件。我的bad@SumitKumar当用户按“添加新自定义”按钮时,我想清除表单。我使用的是组件而不是控制器。这是一样的:我不想通过用户单击来重置表单,但当用户单击表单中未嵌套的按钮时,这与我尝试的有何区别ctrl.userForm.$setPristine;`他在回答中也使用了这一点。我不同意,我有一个类似的问题,我所建议的是解决我问题的方法。为什么这不是问题的答案?
(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");
}
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<form name="myfrom">
<input type="text" ng-model="data.name">
<input type="text" ng-model="data.phone">
<input type="text" ng-model="data.city">
<input type="button" ng-click="reset_from()" value="reset">
</form>
{{data}}
</body>
</html>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.data = {name:"Sahed sawon",phone:"8801714999720",city:"Dhaka"};
$scope.reset_from = function() {
$scope.data = {};
}
});
</script>