gpt4 book ai didi

javascript - ng-click 后 angular 不会重置表单?

转载 作者:行者123 更新时间:2023-11-29 19:00:19 25 4
gpt4 key购买 nike

单击 ng-click 后我的表单不会重置,我做对了吗?

我可以正常提交,但表单不会重置,这是我的日志:

angular.js:12701 POST http://127.0.0.1:8000/auth/post 500 (Internal Server Error)

但我的帖子提交正常,只是没有重置。

jsfiddle

https://jsfiddle.net/2fyynf6p/

主要.js

var app = angular.module('eli', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');

});

app.controller('mainCtrl', ['$scope', '$http', function($scope, $http){


$scope.posts = {};

$scope.addPost = function(){

$http.post('/auth/post', {
title: $scope.post.title,
body: $scope.post.body

}).then(function(data, status, headers, config){
$scope.posts.push(data);
$scope.postform = "";


});


};


}]);

html

            <form  name="postform" method="POST" novalidate>
{{ csrf_field() }}
<div class="form-group">
<label for="post.title">Title</label>
<input ng-model="post.title" class="form-control" type="text" name="title" placeholder="Enter a title" required/>
</div>

<div class="form-group">
<label for="post.title">Post</label>
<textarea ng-model="post.body" type="text" class="form-control" name="body" id="" cols="10" rows="5"></textarea>
</div>

<button id="eli-style-button" ng-click="addPost()" class="btn btn-primary" type="submit">Submit</button>
</form>

最佳答案

可以做这样的事情

$scope.addPost = function({
$http.post('/auth/post', {
title: $scope.post.title,
body: $scope.post.body
}).then(function(data, status, headers, config) {
$scope.posts.push(data);
$scope.post.title = "";
$scope.post.body = "";
});
};

关于javascript - ng-click 后 angular 不会重置表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47276311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com