javascript - 从 Angular Promise 访问 Form 元素和 div

标签 javascript angularjs twitter-bootstrap

我正在学习 Angular JS。我只想清除表单字段并在 http then() 中显示成功的 div。

this.formSubmitted = false;
this.successs = false;

myResumeApp.controller("FormController",['$http', function($http){
    this.formSubmit = function(contactForm) {
    this.formSubmitted = true;
    if(contactForm.$valid)
    {
        $http({
              method: 'post',
              url: 'http://jerrythimothy.is-great.net/mailme.php',
              data: $.param({
                    fname : this.fname,
                    email : this.email,
                    content : this.content
                }),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {
                this.successs = true;
                // this callback will be called asynchronously
                // when the response is available
              }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
              });// JavaScript Document
        }
    };
}]);



<div class="container" ng-controller="FormController as formCtrl">
  <h2>Contact me</h2>
  <div ng-show="formCtrl.successs" class="alert alert-success fade in"          style="padding-top:5px;padding-bottom:5px;margin-top:5px;margin-bottom:5px;">Thank you for contacting me. I will be in touch with you shortly.</div>
   <form role="form" name="contactForm" novalidate ng-submit="formCtrl.formSubmit(contactForm)">

请告诉我我的代码是否有任何问题或任何其他建议。控件位于 then() block 内。但我需要知道如何访问成功元素并清除表单字段。

谢谢。

最佳答案

使用 $scope(并将其添加到依赖项中),而不是 this。正确知道您的 successs 属性已分配给不同的对象(窗口和回调函数)。

Controller 代码:

myResumeApp.controller("FormController",[
    '$scope',
    '$http', 
    function($scope, $http){    
        $scope.successs = false;
        $scope.formSubmitted = false;
        $scope.msg = {};
        $scope.formSubmit = function(contactForm) {
            $scope.formSubmitted = true;

            if(contactForm.$valid)
            {
                $http({
                  method: 'post',
                  url: 'http://jerrythimothy.is-great.net/mailme.php',
                  data: $.param({
                        fname : $scope.msg.fname,
                        email : $scope.msg.email,
                        content : $scope.msg.content
                    }),
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function successCallback(response) {
                    $scope.successs = true;
                    $scope.msg = {};
                    // this callback will be called asynchronously
                    // when the response is available
                  }, function errorCallback(response) {
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                  });// JavaScript Document
            }
        };  
    }
]);

HTML 代码:

<div class="container" ng-controller="FormController as formCtrl">
<h2>Contact me</h2>

<div ng-show="successs" 
class="alert alert-success fade in"          
style="padding-top:5px;padding-bottom:5px;margin-top:5px;margin-bottom:5px;">
    Thank you for contacting me. I will be in touch with you shortly.
</div>
<form role="form" name="contactForm" novalidate ng-submit="formSubmit(contactForm)">
    <input type="text" name="name" ng-model="msg.fname" />
    <input type="text" name="email" ng-model="msg.email" />
    <input type="text" name="content" ng-model="msg.content" />
    <input type="submit" value="submit" />
</form>

关于javascript - 从 Angular Promise 访问 Form 元素和 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35049129/

相关文章:

javascript - 如何将 Plupload 与 Node.js 一起使用并显示上传百分比?

javascript - 某些网页无法在 JavaFX webview/webengine 中正确加载

javascript - 为什么数组只包含最后一个值 n 次?

html - 我有一个带有两个 Logo 的导航栏 - 一个不是导航栏品牌的导航栏应该在菜单折叠时与折叠按钮保持一致

javascript - 从下拉菜单中打开弹出窗口

javascript - 使用数组中的值更新对象数据

javascript - Angular ui-router仅刷新页面的一部分,需要多个 View 吗?

javascript - 如何使用 Angular Directive(指令)获取隔离范围的更新值?

javascript - 如何在单击链接时将动态内容添加到 Bootstrap 模式 - Jquery/JS

JavaScript/jQuery 类修改