Is there a way to set a default value on an angularjs model? I'm hoping for a solution similar to the following code:
div.form-multiple(ng-model='story.r || []', form-multiple="form-multiple")
Currently, the story.r
is undefined and I would like to have a predefined value.
Below is my directive. It's interesting that it works when $modelValue already contains an array but not when it's undefined.
.directive('formMultiple', function() {
return {
require: '?ngModel',
link: function(scope, elm, attr, ngModel) {
if (!ngModel)
return
function pushIntoArray () {
if(ngModel.$modelValue && elm.find('input:last').val() != '') { // this works fine
ngModel.$modelValue.push(scope.formMultipleDefault)
scope.$digest()
}
else if (!ngModel.$modelValue) {
ngModel.$modelValue = [scope.formMultipleDefault] // this block does nothing
console.log(ngModel) // returns $modelValue undefined.
}
}
pushIntoArray()
elm.on('blur', 'input', function() {
pushIntoArray()
});
}
};
})