angularjs - Set variable when object changes in Angular -


i'm using angular , attempting show button form when object changes form manipulates.

html:

<input ng-model="form.field1" /> <input ng-model="form.field2" /> <div ng-show="formchanged">show button</div> 

controller:

$scope.form = {     field1: 'hello' } $scope.$watch('form', function(){     $scope.formchanged = true; }); 

i under impression $watch fire whenever part of object changed. seems, however, $watch called @ beginning , once after object changes.

i have plunker here: http://plnkr.co/edit/hv8ollviozslmug2ibxt

parenthetical information: have found if set $watch explicitly @ each property $watch functions expect to, though still run @ beginning.

to stop being called @ beginning i've tried use newvalue , oldvalue variables follows:

$scope.$watch('form', function(newval, oldval){}); 

but doesn't work either.

use $dirty property formcontroller, or set third parameter on $watch callback true object equality comparison.

solution a:

<form ng-submit="submit()" name="myform">   <div>     <input type="email" name="email" ng-model="formdata.email" required />     <span ng-show="myform.email.$dirty && myform.email.$error.required">required</span>   </div>    <div>     <button ng-show="myform.$dirty" type="submit">submit</button>   </div> </form> 

solution b:

$scope.$watch('form', function(){   $scope.formchanged = true; }, true); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -