尝试从输入和复选框中获取 ID,但一直未定义 angularjs

Trying to get ids from input and checkbox but keep getting undefined angularjs

我正在尝试从输入框和复选框中获取 ID,但我一直未定义。有效负载中显示的唯一值是复选框的值。

html

<li class="ui-state-default" ng-repeat="todo in todos">
    <label>
        <span ng-click="toggleChecked(todo.taskId, todo.done)">
            <input type="hidden" name="taskId" ng-model="todo.taskId" value="todo.taskId" />
            <input type="checkbox" name="done[]" ng-model="todo.done" value="todo.done"  ng-checked="todo.done" />
        </span>
        <span ng-class="{'done':todo.done}" ng-cloak>
            <input type="text" ng-model="todo.title" class="todoList" />
            <span class="label label-success finished">Done</span>
        </span>
    </label>
</li>

angularjs

$scope.toggleChecked = function(taskId, done) {

    $scope.done = !$scope.done;

    $scope.todo = {
      taskId: $scope.taskId,
      done: $scope.done
    };


    $http.post('/dashboard/checked', $scope.todo).success(function() {
      alert($scope.todo);
    });
};

请求负载

我的objective是为了能够得到"taskId""done"值传递给angular和angular 将值发送到 laravel 的控制器,该控制器根据 taskId 更新数据库中的复选框值。但如上图所示,我只能获得 "done" 值。

您的代码正在处理两次点击,这会将您的值重置回 false,因此请将您的代码更改为:

<li class="ui-state-default" ng-repeat="todo in todos">
    <label>
        <input type="checkbox" name="done" ng-model="todo.done" ng-change="toggleChecked(todo)" />
        <span ng-class="{'done':todo.done}" ng-cloak>
            <input type="text" ng-model="todo.title" class="todoList" />
            <span class="label label-success finished">Done</span>
        </span>
    </label>
</li>

以及您的控制器代码:

$scope.toggleChecked = function(todo) {
    $http.post('/dashboard/checked', todo).success(function() {
      alert(todo);
    });
};

您可以直接使用 ng-change 观看 check/uncheck 项。此外,如果您使用 ng-model,则无需手动设置值。

干杯!