javascript - Accessing Form elements and div from Angular Promise -
i learning angular js. want clear form fields , show success div inside http then().
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; // callback called asynchronously // when response available }, function errorcallback(response) { // called asynchronously if error occurs // or server returns response error status. });// javascript document } }; }]); <div class="container" ng-controller="formcontroller 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 contacting me. in touch shortly.</div> <form role="form" name="contactform" novalidate ng-submit="formctrl.formsubmit(contactform)"> please let me know whether there wrong code or other suggestions. control coming inside then() block. need know how access successs element , clear form fields.
thank you.
instead of this, use $scope (and add dependencies). right know successs property assigned different objects (window , callback function).
controller code:
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 = {}; // callback called asynchronously // when response available }, function errorcallback(response) { // called asynchronously if error occurs // or server returns response error status. });// javascript document } }; } ]); html code:
<div class="container" ng-controller="formcontroller 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 contacting me. in touch 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>
Comments
Post a Comment