I want to change ng-model(degree) value to id(degree_id) in my template as following
<md-select flex class="md-select-form" ng-model="education.degree" placeholder="Degree" save-id required-param="{{education.degree_id}}">
<md-option ng-value="degree" ng-repeat="degree in ['High School', 'Associates Degree', 'Bachelor Degree', 'Masters Degree', 'M.B.A', 'M.D', 'Ph.D', 'other']">{{degree}}</md-option>
</md-select>
I have used the directive(save-id) for this parsing in my controller and i have pulled degree service data and binding the relevant id.The code is following
.directive('saveId', function(Profile){
return {
require: 'ngModel',
scope: {
requiredParam:'@'
},
link: function(scope, element, attrs, ngModel) {
console.log("initial loading");
// view --> model (change to string)
ngModel.$parsers.push(function(viewValue){
var id = -1;
var keepGoing = true;
Profile.getDegreeList().then(function(data) {
angular.forEach(data, function(ob){
if(keepGoing) {
if(ob.degree == viewValue){
id = ob.degree_id;
keepGoing = false;
}
}
});
//console.log(id); // it gives updated value
});
console.log(id); // it gives -1
return id;
});
}
};
})
I have updated the model value as mentioned above the problem is ,Only the updated value is available within
Profile.getDegreeList(){ }
Out of this function the value is -1 , what's wrong here? Please give me the solutions??