I am trying to accomplish a simple valid toggle on the outcome of a keyup function. Basically, an input keyup action triggers a function. It does some stuff, then returns true or false. Upon this, the css class of span near the input should changes. Am I missing something in ng-class? I have searched for a similar topic but still couldn't overcome my problem.
html code:
<div ng-repeat="drug in inputDrugs track by $index">
<label>Type drug name</label><br/>
<input ng-keyup="getRxcui(medValue, $index)" ng-model="medValue" placeholder="something like Advil" />
<span class="alert alert-danger" role="alert" ng-class="{'alert alert-success': getRxcui(medValue, $index)}">?</span>
</div>
in controller.js:
$scope.getRxcui = function(medValue, index){
var rxcui = '',
url = 'blabla' + medValue;
$http.get(url).
success(function(data, status){
try {
// code here goes here and then..
return true
}
catch(err) {
// If value is not correct:
return false
}
}).
error(function(data, status){
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('error receiving data: ' + status);
console.error(data);
});
}
I have managed to to so easily with jQuery, but the point for me is to keep angular code clean. Any help is appreciated, thanks.
For one, you don't need to "override" the class you can have only one. Anyhow, the problem is that you are returning a value from a Promise, and it's meaningless.
Also, you have to associate the status of the request with the loop iterator so first change the markup to this:
Now have this for the span:
And finally in your function: