Angular:如何在控制器中获取表单的输入值?

时间:2014-04-19 02:28:41

标签: angularjs

我有一张表格:

<form ng-submit="submit()">
    New Chore: <br>
    Name: <input type="text" name="name"><br>
    Hours: <input type="text" name="hours"><br>
    <input type="submit" id="submit" value="Submit" />
</form>

并在我的控制器中:

$scope.submit = function() {
    if ($scope.name) {
        alert("here");
        $scope.chores.push({name: $scope.name, hours: $scope.hours});
        $scope.name = '';
    } else {
        alert("none");
    }
};

但它始终是else语句。如何在控制器中获取表单中的名称和小时数?

1 个答案:

答案 0 :(得分:2)

您没有将输入值绑定到示波器的属性。

尝试ng-model

<form ng-submit="submit()">
    New Chore: <br>
    Name: <input type="text" name="name" ng-model="name"><br>
    Hours: <input type="text" name="hours" ng-model="hours"><br>
    <input type="submit" id="submit" value="Submit" />
</form>