无法清除 Angular 中 ngDialog 模板中的表单输入值
Can't clear form input values in ngDialog template in Angular
我正在使用 ngDialog [ https://github.com/likeastore/ngDialog ] 创建一个模式,我有一个基本表单,当我单击一个按钮时它会发出 POST 请求。在同一个按钮上单击,我希望清除表单输入。通常你可以做类似 $scope.valueOfNgModel = "";
的事情,但在这种情况下这是行不通的。此外,ngDialog 作用域与其实例化控制器的 $scope 相同。
这是 ngDialog 实例化:
$scope.addStudents = function() {
ngDialog.open({
template: 'addStudents',
scope: $scope
});
};
这是 ngDialog 模板:
<script type="text/ng-template" id="addStudents">
<h1> Add Students to Your Class </h1>
<form ng-model="myForm" ng-submit="addNewStudent(newStudent)">
<input ng-model="newStudent.firstName" placeholder="First Name">
<input ng-model="newStudent.lastName" placeholder="Last Name">
<input ng-model="newStudent.email" placeholder="Email">
<input ng-model="newStudent.image" placeholder="imageUrl">
<h3> Note: Student Password by Default will be Firstname.lastname </h3>
<button class="ngdialog-button
ngdialog-button-primary"
type="submit">Add Student
</button>
</form>
</script>
下面是在表单提交(或按钮单击)上运行的函数,我想清除所有输入字段:
$scope.addNewStudent = function(student) {
var newUserToAdd = {
firstName: student.firstName,
lastName: student.lastName,
teacher: false,
email: student.email,
password: student.firstName + '.' + student.lastName,
classesBelongTo: [$scope.currentClassId],
image: student.image
};
userService.postNewUser(newUserToAdd)
.then(function(response) {
$scope.loggedInUser = response.data;
})
console.log("this is $scope.newStudent", $scope.newStudent); //this is undefined - I'm not sure why
$scope.newStudent = {}; //I tried this to clear it out
$scope.$apply(); //I tried this to no avail
}
非常感谢任何帮助!
您可以为表单命名,然后调用 $scope.yourFormName.$setPristine();
例子:Fiddle
我正在使用 ngDialog [ https://github.com/likeastore/ngDialog ] 创建一个模式,我有一个基本表单,当我单击一个按钮时它会发出 POST 请求。在同一个按钮上单击,我希望清除表单输入。通常你可以做类似 $scope.valueOfNgModel = "";
的事情,但在这种情况下这是行不通的。此外,ngDialog 作用域与其实例化控制器的 $scope 相同。
这是 ngDialog 实例化:
$scope.addStudents = function() {
ngDialog.open({
template: 'addStudents',
scope: $scope
});
};
这是 ngDialog 模板:
<script type="text/ng-template" id="addStudents">
<h1> Add Students to Your Class </h1>
<form ng-model="myForm" ng-submit="addNewStudent(newStudent)">
<input ng-model="newStudent.firstName" placeholder="First Name">
<input ng-model="newStudent.lastName" placeholder="Last Name">
<input ng-model="newStudent.email" placeholder="Email">
<input ng-model="newStudent.image" placeholder="imageUrl">
<h3> Note: Student Password by Default will be Firstname.lastname </h3>
<button class="ngdialog-button
ngdialog-button-primary"
type="submit">Add Student
</button>
</form>
</script>
下面是在表单提交(或按钮单击)上运行的函数,我想清除所有输入字段:
$scope.addNewStudent = function(student) {
var newUserToAdd = {
firstName: student.firstName,
lastName: student.lastName,
teacher: false,
email: student.email,
password: student.firstName + '.' + student.lastName,
classesBelongTo: [$scope.currentClassId],
image: student.image
};
userService.postNewUser(newUserToAdd)
.then(function(response) {
$scope.loggedInUser = response.data;
})
console.log("this is $scope.newStudent", $scope.newStudent); //this is undefined - I'm not sure why
$scope.newStudent = {}; //I tried this to clear it out
$scope.$apply(); //I tried this to no avail
}
非常感谢任何帮助!
您可以为表单命名,然后调用 $scope.yourFormName.$setPristine();
例子:Fiddle