angularjs - Angular - default value for model -


say have html follows:

<html> <head> angular etc. </head> <body ng-app> <div ng-controller="myctrl">        <input ng-model="weight" type="number" min="{{minweight}}" max="{{maxweight}}">       <p>{{weight}}</p> </div> </body> </html> 

and following controller:

function myctrl($scope){     $scope.weight = 200;     $scope.minweight = 100.0;     $scope.maxweight = 300.0;   }  

the "min" , "max" show user input bad, , if hard code them say, 100 , 300, make sure value never bound model @ (why isn't behavior same??). i'd change "weight" if value meets input requirements. thoughts?

i don't understand trying do.

html:

<html> <head> angular etc. </head> <body ng-app="myapp"> <div ng-controller="myctrl">        <input ng-model="weight" type="number" min="{{minweight}}" max="{{maxweight}}">       <p>{{weight}}</p> </div> </body> </html> 

angular: [edit]

var app = angular.module('myapp', []);  app.controller('myctrl', ['$scope', function myctrl($scope){     $scope.weight = 200;     $scope.minweight = 100.0;     $scope.maxweight = 300.0;      $scope.$watch('weight', function (newvalue, oldvalue) {         if(typeof newvalue === 'number') {             if(newvalue > $scope.maxweight || newvalue < $scope.minweight) {                 $scope.weight = oldvalue;             }         }     }); }]); 

but here example made in jsfiddle. hope solution looking for.

[edit]

http://jsfiddle.net/migontech/jfdd2/1/

[edit 2]

i have made directive delayed validation of input field. , if incorrect sets last correct value. totally basic. can extend if less allowed use min value, if more allowed use max value you. can change directive like.

http://jsfiddle.net/migontech/jfdd2/2/

if have questions let me know.


Comments