javascript - AngularJS: Target a form with Controller As syntax in an object -


note: did around here on solutions, yet no 1 had additional issue of function being in object.

i have form in angular js app:

<div ng-app="plunker">   <div ng-controller="pmtcontroller pmt">    <form name="myform">     <div class="form-group">      <input type="text" class="form-control" />     </div>     <button class="btn btn-primary" ng-click="pmt.search.resetsearchform()">reset</button>    </form>  </div> </div> 

further, have controller object:

app.controller('pmtcontroller', function($log) {   var _this = this;    _this.search = {     resetsearchform: function () {      $log.debug('test');      // how target form?    }  }; }) 

my ng-click works, log.debug works. no amount of tweaking target form can reset entire thing (empty fields) works.

i can $window.myform.reset(); how angular?

note please main issue/question how correctly target form inside resetsearchform function in search object.

note tried changing form name pmt.myform or pmt.search.myform no avail.

i tried $setpristine , $setuntouched() don't seem clear fields.

i know can assign model , assign form controls, prototype i'd rather simple reset.

i made pen: https://codepen.io/smlombardi/pen/ywoppq?editors=1011#0

here take on codepen resolve issue:

https://codepen.io/watsoncn/pen/ywoxqz?editors=1011

explanation:

angular's documentation provides example of "form reset" button, can apply same logic towards resetting after submission:

documentation:https://docs.angularjs.org/guide/forms

with plunker:

live example:https://plnkr.co/edit/?p=preview

the example shows use of angular's copy method creates deep copy of whatever pass parameter , assigns ng-model put on particular input field. in case pass empty master object.

you need make sure add ng-model attribute inputs, create reset function can run after submission. common option set each input's ng-model empty strings in submission function, such $scope.inputmodel = ""

is hoping for? might have misunderstood question. happily take crack @ if there still confusion.


Comments