angularjs - Angular Form Validation and Sending HTML Template with Form -
here plunkr demonstrating issue.
i apologize if seems should 2 separate questions, seems me issues related.
i have form requires name , email address, , when submitted, sends those, in addition html email template, end. here abbreviated version of form:
<form novalidate id="send-email" data-ng-controller="sendlistcontroller" ng-submit="sendemail()"> <div class="form-group"> <!-- hidden input populated compiled html controller --> <input type="hidden" id="htmltemplate" name="htmltemplate" ng-value="form.compiledhtml" data-ng-model="form.compiledhtml"> </div> <div class="form-group" ng-class="{'has-error':form.name.$invalid && form.name.$dirty}"> <input type="text" class="form-control" id="name" name="name" placeholder="full name" data-ng-model="form.name" required> <span class="help-block has-error" ng-if="form.name.$dirty">this field required</span> </div> <div class="form-group"> <button type="submit" ng-disabled="loading"> <span ng-show="!loading"> <span class="glyphicon glyphicon-send"></span> mail it! </span> <span ng-show="loading"> <span class="fa fa-spinner fa-spin" ng-show="loading" ></span> sending . . . </span> </button> </div> </form> the loading , !loading functionality working, , hidden inputs populated properly, , communication end fine. error classes being added elements, form errors never shown , form submits when required inputs have classes ng-dirty , ng-invalid.
here controller (and, way, don't looking @ html markup in js, if has better ideas, please let me know):
app.controller('sendlistcontroller', ['$scope', 'list', '$http', '$compile', '$timeout', 'toastr', function ($scope, list, $http, $compile, $timeout, toastr) { $scope.form = {}; $scope.loading = false; var container = angular.element('<div></div>'); // list service $scope.list = list; container.html( '<h1>{{list.listtitle}}</h1>' + '<ul>' + '<li data-ng-repeat="item in list.show()">' + '<span> {{item.displayname}} — </span>' + '<span> {{item.description}} — </span>' + '<span> {{item.count}} {{item.counttext}} — </span>' + '<span> ${{item.price}} </span>' + '</li>' + '</ul>' ); $compile(container)($scope); $timeout(function () { $scope.form.compiledhtml = container.html(); // htmltoplaintext() private method definition have not // included here. trust works. $scope.form.plaintext = htmltoplaintext(container.html()); }); $scope.sendemail = function() { $scope.loading = true; $http.post('/mail-it', $scope.form) .success(function (response) { if (response.successmessage) { toastr.success('success', response.successmessage); } else { toastr.warning(response.errormessage); } $scope.form = {}; $scope.loading = false; }) .error(function (response) { toastr.error('error', 'uh-oh! went wrong. try again!'); $scope.loading = false; }); } again, working (which, know, doesn't mean it's or right), except form validation. somehow related injecting compiled html, , in either case, there better way send template along form?
Comments
Post a Comment