javascript - Using ng-init and ng-model together -


hi have situation have format values using ng-init have ng-model 2 way binding in input field value can changed , saved.

<div ng-repeat="ab in ablist">     <div class="col-sm-3">         <input type="text" ng-init="item.ab=fn(item.ab)"  ng-model="item.ab" />     </div>  </div> 

the above code doesnt work. not show formatted value. can let me know how can change can display formatted value using ng-init keep ng-model binding edited value on submit.

in case have use directive. ng-init used when want set default or initial value control. directive can keep display value , model value in separate formats. here directive formatting integers commas.

 app.directive('formattednumber', function () {     return {         link: function (scope, element, attrs, ctrl) {             element.bind('blur', function (blurevent) {                 if (element.data('old-value') !== element.val()) {                     // console.log('value changed, new value is: ' + element.val());                     scope.$apply(function () {                         var v = number_format(element.val(), 0, 0, 99999999999);                         element.val(v);                         //scope.mydirective = element.val();                         //element.data('old-value', element.val());                     });                 }             });             ctrl.$formatters.unshift(function (modelvalue) {                 if(!modelvalue) modelvalue="0";                 if(modelvalue.replace &&(isnan(parsefloat(modelvalue)) || !isfinite(modelvalue)))                 {                     modelvalue =  parsefloat(modelvalue.replace(/,/g, ""));                 }                 var v = number_format(modelvalue, 0, 0, 99999999999);                 v = v == undefined? 0 : v;                 element.val(v);                 return v;             });             ctrl.$parsers.unshift(function (viewvalue) {                 if(!viewvalue) viewvalue="0";                 viewvalue =  parseint(viewvalue.replace(/,/g, ""));                 var v = number_format(viewvalue, 0, 0, 99999999999);                 v = v == undefined? 0 : v;                 element.val(v);                 return viewvalue;             });         },         restrict: 'a',         require: 'ngmodel'     } }); 

similarly, can write 1 date formatting. can use date.js same.

the usage :

<input type="text" ng-model="item.amount" formattednumber /> 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -