1

I have my DOM element as :

<i id= class="icon clr-org fleft ion-bookmark" ng-class="{business.class : setFavouriteBusinessIcon(business.ID, $index)}"></i>

now I want to apply business.class which is a variable in my scope as the element's class whenever setFavouriteBusinessIcon(business.ID, $index) returns true.

but the current ng-class condition is not working.

1
  • class="{{className}}" this works fine Commented Apr 19, 2017 at 10:41

1 Answer 1

1

You can do so by having single quotes and {{...}} around it. Like this:

<i ng-class="{'{{business.class}}' : setFavouriteBusinessIcon(business.ID, $index)}"></i>

Here's a working example: (notice that world class has been applied and so is its CSS)

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'world';
});
.world {
  background-color: red;
}
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
</head>

<body ng-controller="MainCtrl">
  <p ng-class="{'{{name}}': true}">Hello {{name}}!</p>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.