javascript - AngularJS textbox value undefined -
i have created directive wherein copies value of textbox1 textbox2.
function mycopytext() { return { restrict: 'a', link: function(scope, element, attr) { $('#textbox2').val($('#textbox1').val()) } } } then on textbox field:
<input type="text" id="textbox1" ng-model="vm.textbox1" my-copy-text /> <input type="text" id="textbox2" ng-model="vm.textbox2" /> it works fine until submitted form wherein vm.textbox2 undefined. if manually inputted value on textbox2, vm.textbox2 able display value.
i find weird when directive value assignment, vm.textbox2's value undefined not until manually set value typing in.
that's design. angular digest kicks in when change value via input/change event therefore val() not trigger setviewvalue, model value not updated.
this directive replicate value model/view name pass in replicate-to attribute:
function replicateto($parse) { return { scope: false, require: 'ngmodel', restrict: 'a', link: function(scope, element, attr, ngmodelctrl) { var target = $parse(attr.replicateto); var angularrender = ngmodelctrl.$render; ngmodelctrl.$render = function(){ angularrender(); target.assign(scope, ngmodelctrl.$viewvalue); }; ngmodelctrl.$viewchangelisteners.push(function(){ target.assign(scope, ngmodelctrl.$viewvalue); }); } } } <input type='text' ng-model="vm.textbox1" data-replicate-to="vm.textbox2"/> <br>
Comments
Post a Comment