I have a javascript object suppose user is the name of my object and in angular js it looks like this
$scope.user = {
name:'Arpit',
class:'Computer Science',
year:'2017',
gender:'male'
}
This object I am fetching from the database and opening in the edit screen, now if in the HTML form if any field got changed by the user in edit mode I want to highlight the particular field using my CSS class applyborder. My logic is working for the first time when I am changing any field value but when I reset the value as original the class should be removed, but it is not removing the class. My angular js code is:
//Value Change Detection
Object.keys($scope.user).filter(function (key) {
$scope.$watch('user.' + key, function (newVal, oldVal) {
if (newVal != oldVal) {
var ele = $('[ng-model="user' + '.' + key + '"]');
ele.addClass("applyborder");
}
else if(oldVal == newVal){
var ele = $('[ng-model="user' + '.' + key + '"]');
ele.removeClass("applyborder");
}
});
});
It is treating last entered value as oldVal but it should treat to the value which comes from the database. Thanks.
else if(oldVal == newVal){is the two spaces a typo?