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

2

2 Answers 2

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

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

Comments

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

1 Comment

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?

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.