I am writing a ui control that uses ngModel:

<foo ng-model="myModel"> I would like to ensure that ngModel.$modelValue is 
never empty and would like to implement the following logic inside the 
directive: if $modelValue is empty (including programmatically) -> set to 
internal default model. To be clear, it is not enough for $viewValue not to 
be empty. I would like $modelValue to not be empty as well. 

Tried invoking $setViewValue(DEFAULT_VIEW) from a) $parser whenever model 
is empty; b) a $watch watching $modelValue, but do not see $modelValue 
being updated to a default model (from hard-coded DEFAULT_VIEW. What 
approach should I take?

thanks,
-nikita


function link(scope, ngModel){

var DEFAULT_VIEW = {};
ngModel.$formatters.unshift(modelToView);
ngModel.$parsers.push(viewToModel);

//init 
ngModel.$setViewValue(DEFAULT_VIEW);

function modelToView(m){
if(!m) {
 ngModel.$setViewValue(DEFAULT_VIEW); //no apparent effect on $modelValue
};

//handle non-empty case
}

//also tried watching $modelValue
scope.$watch(function(){return ngModel.$modelValue;}, function(mv, oldMv){
if(!mv)
{
 ngModel.$setViewValue(DEFAULT_VIEW); //no apparent effect on $modelValue
}
});


}

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to