I have an app where I am running ng-repeat to display information. I need to then add classes to some of the generated elements after an Ajax call has been made.
I could do this easily with jQuery but I'm trying to stick to Angular/jqlite.
The problem I'm having is that I can get the element, but not as an object that addClass works on.
Here's what I've got so far:
angular.forEach($(".tile"), function(tile){
if(srv.free.indexOf(angular.element(tile.querySelector('.label')).text()) != -1){
tile.addClass("free");
}
});
The array srv.free contains a list of names, those name values are the same as the text value of the div with class .label, which is inside of .tile. So I need to loop through each .tile, check the text value of the child .label and if that text value is in the array srv.free, add the class .free to .tile.
The point I'm at, is that addClass is "undefined" because at this point, tile is just a string, not a jquery/jqlite object.
How do I add a class to that, or get to the object version?
Update
I have previously tried to use ng-class on the elements to update the class, but could not get them to update.
I have a service that has free in it, which is initially set to a blank array. After an Ajax call:
$http.get('/json/free.json').success(function(data){
srv.free = data;
});
Then, in my controller I have:
$scope.gsrv = globalService;
and in my ng-repeat:
<div class="col-xs-3 col-md-2" ng-repeat="Tile in list.tiles">
<div class="tile" id="{{$index}}" ng-class="{free:$.inArray(Tile.Name, gsrv.free)}" ng-click="main.changeView('/Tile/'+$index)">
<img src="http://placehold.it/256" ng-src="{{Tile.Icon}}" ng-class="{dis:!Tile.Stats}">
<div class="label">{{Tile.Name}}</div>
</div>
</div>
When that didn't work, I tried adding:
$scope.gsrv.free = globalService.free;
which did not change anything.