Ada*_*abo 3 validation angularjs typescript
当我尝试使用form.$setPristineTypeScript时,它不起作用,调试说form是未定义的.根据我的阅读,$scope.formName.$setPristine()将形式设置为原始.要从$scope.formName控制器访问,我将其作为ng.IFormController属性添加到我的自定义范围界面.但是,当我调用时$scope.form.$setPristine(),它不起作用,并且调试节目$scope.form未定义.
打字稿:
interface IMyScope extends ng.IScope {
employees: Array<IEmployee>;
employeeToAdd: IEmployee;
addEmployee(): void;
form: ng.IFormController;
}
class EmployeeAddController {
static $inject = ['$scope', 'Employees'];
constructor(
$scope: IMyScope,
$modalInstance: ng.ui.bootstrap.IModalServiceInstance,
Employees: IUpdateableResourceClass<IUpdateableResource>
) {
$scope.employees = new Array<IEmployee>();
$scope.employeeToAdd = {
Name: '',
Email: ''
};
$scope.addEmployee = function () {
Employees.save(null, $scope.employeeToAdd, function (employee: IEmployee) {
$scope.employees.push(employee);
$scope.employeeToAdd.Email = '';
$scope.employeeToAdd.Name = '';
$scope.form.$setPristine(); // $scope.form is undefined
});
};
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<form role="form" class="form-inline" name="form">
<div class="form-group" ng-class="{ 'has-error': form.name.$dirty && form.name.$invalid }">
<input type="text" class="form-control" ng-model="employeeToAdd.Name" name="name" required>
</div>
<div class="form-group" ng-class="{ 'has-error': form.email.$dirty && form.email.$invalid }">
<input type="email" class="form-control" ng-model="employeeToAdd.Email" name="email" required>
</div>
<button type="button" class="btn btn-default" ng-click="addEmployee()" ng-disabled="form.$invalid">Add</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我在使用表单的应用程序中所做的是将表单对象传递给清除它的方法,而不是直接处理作用域.
<button data-ng-click="clearAndResetForm(Form)"></button>
$scope.clearAndResetForm = (form:ng.IFormController)=> {
this.clearAndResetForm(form);
};
private clearAndResetForm(form:ng.IFormController) {
this.$scope.fieldOne = undefined;
this.$scope.fieldTwo = undefined;
form.$setPristine();
}
Run Code Online (Sandbox Code Playgroud)
我不完全确定为什么我的代码会工作,而你的代码却没有.但希望这种方法可以帮助你.
| 归档时间: |
|
| 查看次数: |
9871 次 |
| 最近记录: |