AngularJS在POST后不清除数组



问题

你好!当我将数据POST到后端时,一切都很好,但当我点击ENTER并POST一个空字段时,它会再次POST相同的值。在发布任何内容之前,我不能POST空字段,但在我输入一次vaule然后发送空字段之后,我可以POST相同的值,比如99999 x次。如果在发送第一个值后,我输入了新值,它将是POST新值的一部分,但我可以通过在空字段上单击enter再次发布新值999999次。作为后端,如果这些信息可以帮助解决方案,我使用laravel 5。

我的代码

HTML

<h1>{{ tasksdata.lists.name }}</h1>
<hr/>
    <div ng-repeat="task in tasksdata.tasks">
    <h2>{{ task.description }}</h2>
    </div>
<h2><a href="javascript:hideshow(document.getElementById('adiv'))"> + </a></h2>
<input type="text" ng-model="taskname" id="adiv" ng-keydown="key($event)" value="" onkeydown="hideshow(document.getElementById('adiv'))" class="form-control" style="font:24px bold; display: none" placeholder="NEW TASK" />

编写脚本

function hideshow(which){
if (which.style.display=="none"){
    which.style.display="block";
    which.value="";
    which.focus();
}
$(which).keydown(function(e) {
    if (e.keyCode == 13) {
        which.style.display = "none"
    }
});
$(which).blur(function () {
    which.style.display = "none"
});
}

POST 的角度

myApp.controller('tasksController', ['$scope', '$log', '$http', '$routeParams',
    function($scope, $log, $http,$routeParams ){
        $http({
            method: 'GET',
            url: 'http://localhost/anydocopy/public/lists/' + $routeParams.id
        })
            .success(function (d) {
                console.log(d);
                $scope.tasksdata = d;
            });
            var task = {};
        console.log('before sending');
        console.log(task);
        $scope.key = function($event){
            console.log($event.keyCode);
            if ($event.keyCode == 13) {
                task = {
                    description: $scope.taskname,
                    list_id: $routeParams.id
                };
                $http({
                    method: 'POST',
                    url: 'http://localhost/anydocopy/public/lists/'+$routeParams.id,
                    data: task
                })
                    .success(function () {
                        console.log('true');
                        task = {};
                        console.log('after sending');
                        console.log(task);
                        $http({
                            method: 'GET',
                            url: 'http://localhost/anydocopy/public/lists/' + $routeParams.id
                        })
                            .success(function (d) {
                                console.log(d);
                                $scope.tasksdata = d;
                            });

                    })
                    .error(function(){
                        console.log('false');
                    })
            }};
    }]);

我尝试了什么

我在发布之前定义了新的任务数组,然后用html中的数据填充它,并对它们进行POST。如果POST成功,则数组为空。在控制台中,POST前后的数组总是空的,所以我不明白当我点击ENTER时,它怎么能发送和以前一样的数据。。而且每次我显示输入文本时,它的值都是"。

尝试设置:$scope.taskname=";当帖子成功时。我认为双向绑定是将输入字段设置回以前的条目,但由于显示的原因,您看不到它:none。如果您使用开发人员工具,您可能会看到输入字段包含与以前相同的值。

最新更新