我有一个简单的形式:
<form name="add-form" data-ng-submit="addToDo()">
<label for="todo-name">Add a new item:</label>
<input type="text" data-ng-model="toDoName" id="todo-name" name="todo-name" required>
<button type="submit">Add</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我的控制器如下:
$scope.addToDo = function() {
if ($scope.toDoName !== "") {
$scope.toDos.push(createToDo($scope.toDoName));
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是在提交后清除文本输入,所以我只需清除模型值:
$scope.addToDo = function() {
if ($scope.toDoName !== "") {
$scope.toDos.push(createToDo($scope.toDoName));
$scope.toDoName = "";
}
}
Run Code Online (Sandbox Code Playgroud)
除了现在,因为表单输入是"必需的",我在表单输入周围得到一个红色边框.这是正确的行为,但不是我想要的在这种情况下......所以我想清除输入然后模糊输入元素.这导致我:
$scope.addToDo = function() {
if ($scope.toDoName !== "") {
$scope.toDos.push(createToDo($scope.toDoName));
$scope.toDoName = "";
$window.document.getElementById('todo-name').blur();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道在Angular文档中对这样的控制器中的DOM进行修改是不受欢迎的 - 但是有更多的Angular方法吗?
angularjs ×1