1

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

user3293629
  • 119
  • 1
  • 8
  • 2
    use ng-class https://docs.angularjs.org/api/ng/directive/ngClass – Subin Sebastian Jul 13 '14 at 19:20
  • possible duplicate of [What is the best way to conditionally apply a class with angularjs?](http://stackoverflow.com/questions/7792652/what-is-the-best-way-to-conditionally-apply-a-class-with-angularjs) – drew_w Jul 13 '14 at 19:39

2 Answers2

2

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

sylwester
  • 16,498
  • 1
  • 25
  • 33
0

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

V31
  • 7,626
  • 3
  • 26
  • 44
  • I could do that, but there are a number of classes i want to switch between and that seems very inefficient, i would need a bool for each and an ng-class attribute for each, is there no way to do it with a variable string? – user3293629 Jul 13 '14 at 20:20