Tuesday 15 July 2014

javascript - How to get field that made form dirty in angular? -



javascript - How to get field that made form dirty in angular? -

is there way know field made form dirty in angular? have "auto save" directive checks if there alter within form, , saves accordingly (trough server request), code follows:

app.directive 'nxautosave', ($timeout) -> homecoming { scope: { nxerror: '=' nxautosave: '&' } require: ["^form"] link: (scope, element, attrs, ctrls) -> dirtyelement = null $formctrl = ctrls[0] savepromise = null scope.$watch -> if $formctrl.$valid , $formctrl.$dirty $timeout.cancel savepromise if savepromise savepromise = $timeout(-> savepromise = null # still valid? if $formctrl.$valid scope.nxautosave() $formctrl.$setpristine() homecoming , 1100) homecoming scope.$watch 'nxerror', (newval, oldval) -> #???

}

the directive watches nxerror (used callback) know if there has been error sent server. within method, want display popover error message, need know element made form dirty in order activate popover...

does have suggestions?

each named input within form has dirty state too. if set name attribute on input elements, available on form check. if have form defined as:

<form name="myform"> <input name="myfield" type="text" ng-model="mymodel" /> </form>

you will, on scope able see:

scope.myform.myfield.$dirty;

hope helps...

javascript angularjs angularjs-directive angularjs-scope

No comments:

Post a Comment