简体   繁体   English

使用angularjs提交后,输入字段不应清除

[英]Input field should not clear after submit using angularjs

HTML HTML

<form ng-controller="updatecontroller" ng-submit="updateUser()"><label class="control-label">First Name</label>
    <input type="text"  ng-model="user.userFirstName">
    <label class="control-label">Last Name</label>
    <input type="text"  ng-model="user.userLastName" ><button type="submit" ng-click="updateUser()">Update</button>
</form> 

JS JS

app.controller('updatecontroller', function ($scope, $http, $cookieStore) {

$http.get('http://localhost:8080/myapp/user/'.concat($scope.getUserId) + '?access_token=' + $cookieStore.get("access_token")).
        then(function (response) {
            $scope.user = response.data;
        });$scope.user = {"id": "","userFirstName": "","userLastName": ""}

$scope.updateUser = function () {

    var url = "http://localhost:8080/myapp/user/".concat($scope.getUserId) + "?access_token=" + $cookieStore.get("access_token");
    var method = "PUT";
    $http({
        method: method,
        url: url,
        data: angular.toJson($scope.user),
        headers: {
            'Content-Type': 'application/json'
        }
    })
};});

values will appear in text field. 值将出现在文本字段中。 i have to update. 我必须更新。 the values are getting updated in database. 值正在数据库中更新。 but what i want is.. the updated values should not clear after submit the form. 但是我想要的是..提交表单后,更新的值不应清除。

Thanks! 谢谢!

将您的Submit Button放置在Form Element中,然后尝试进行操作,它将在提交后清除输入。

your updateUser() method seems to be the problem. 您的updateUser()方法似乎是问题所在。 It's probably clearing user.userFirstName & user.userLastName (or the whole user ) 可能正在清除user.userFirstNameuser.userLastName (或整个user

please show us what updateUser() is doing to be sure 请告诉我们updateUser()在做什么,以确保

You can empty the input filed after get the response from HTTP request

$scope.updateUser = function () {
$http({
    method: 'POST',
    url: 'myUri', 
    data: 'your data'
    headers:'header'
}).then(
    function(res) {
      $scope.user = {"id": "","userFirstName": "","userLastName": ""} //clear the input field here

    },
    function(err) {

    }
);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM