簡體   English   中英

使用angularjs提交后,輸入字段不應清除

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

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

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'
        }
    })
};});

值將出現在文本字段中。 我必須更新。 值正在數據庫中更新。 但是我想要的是..提交表單后,更新的值不應清除。

謝謝!

將您的Submit Button放置在Form Element中,然后嘗試進行操作,它將在提交后清除輸入。

您的updateUser()方法似乎是問題所在。 可能正在清除user.userFirstNameuser.userLastName (或整個user

請告訴我們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