user3293629
user3293629

Reputation: 117

Setting class to a variable in AngularJS

Sorry if this is very simply, i just started using angular yesterday. I want to set a class of something to a variable, yet it isnt working. This is what i've done:

<body ng-app="Witzle" ng-controller="BGController" class="{{bg.type}}">

and

app.controller('BGController',function($scope){
$scope.bg = {type:'witzle-bg-image'};
$scope.gah = function(name){
      alert(name)
      $scope.bg = {type:name};
 }
})

Ive run the code, and i know that $scope.gah() is being called because of the alert, but the class isnt being updated.

UPDATE: gah is already being called from somewhere else, my problem is that when it is called the class doesnt change

Upvotes: 1

Views: 284

Answers (2)

sylwester
sylwester

Reputation: 16498

All you need is use ng-class directive without curly braces.

<body ng-app="Witzle" ng-controller="BGController" ng-class="bg.type">

please see demo:

http://jsbin.com/mesere/1/edit?html,css,js,output

Upvotes: 2

V31
V31

Reputation: 7666

You need to apply ng-class. Created a fiddle to help you to start with coding

Code snippet:

<div ng-class="{abc: setThisClass}">
     TEST
</div>

UPDATED FIDDLE To have the class set according to a variable string

Upvotes: 0

Related Questions