AngularJS - 从Factory方法重置表单输入字段

时间:2015-08-27 21:26:03

标签: angularjs

是否可以在我的工厂提交时重置我的表单?我目前正在重新设置内联,但希望让它更清洁。如果没有,那么更好的方法是什么?

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

app.controller('ToDoController', function($scope, todos){
    $scope.todos = todos;
});

app.factory('todos', function(){
    var todos = {};

    todos.list = [];

    todos.addTodo = function(todo){
        todos.list.push({
            task:todo,
            completed: false
        });
    }    
    return todos;
})

app.directive('todo', function(){
    return {
        restrict: "E",
        templateUrl:'templates/todos.html',
        controller:'ToDoController'
    }
})

// template

<form name="todoForm" novalidate class="text-center">
  <input type="text" ng-model="input">

<!-- it is being reset on the line below  after ngClick-->
            <button ng-click="todos.addTodo(input); input='';" class="btn" type="submit" ng-disabled="!input">Submit</button>
    </form

1 个答案:

答案 0 :(得分:0)

不确定

app.controller('ToDoController', function($scope, todos){
    $scope.addTodo = function() {
        todos.addTodo($scope.input);
        $scope.input = '';
    };
});

并在视图中:

<button ng-click="addTodo()" ...