【问题标题】:Reset Form After Angular $http successAngular $http 成功后重置表单
【发布时间】:2014-09-07 20:40:54
【问题描述】:

我想知道如何在 $http POST 成功后清除联系人对象?代码在成功回调中的 this.contact = {}; 处出错。

angular.module('kbtApp')
  .controller('ContactFormCtrl', function ($scope, $http, toaster) {
        var that = this;
        this.status = false;
        this.contact = {
            'fullname': 'Jim Rainaldi',
            'email': 'jtrainali@gmail.com',
            'organization': 'Jamesrainaldi.com',
            'position': 'Web Guy',
            'message': 'This is just a test.'           
        };
        this.contact.isAjax = true;

        this.sendEmail = function(){
            var status = null;
            var statuss = $http({
                url: "/scripts/php/sendEmail.php",
                method: "POST",
                data: this.contact
            }).success(function(data, status, headers, config) {
                this.contact = {}; 
                toaster.pop('success', "Success", 'Your message has been sent.', 7500, 'trustedHtml');
            }).error(function(data, status, headers, config) {
                //$scope.status = status;
                toaster.pop('error', "Error", 'Email failed to send.', 7500, 'trustedHtml');
            });

        };

  });

【问题讨论】:

  • this.contact 更改为that.contact
  • 为什么不直接使用$scope
  • 当我在成功回调中设置断点时,我在开发工具中输入了 $scope,它返回 undefined。

标签: angularjs forms http reset


【解决方案1】:

@raina77ow 是正确的。我将 this.contact = {} 更新为 that.contact = {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2015-12-04
    • 2021-04-04
    • 2013-03-18
    • 2018-02-04
    相关资源
    最近更新 更多