将类设置为AngularJS中的变量

时间:2021-07-01 22:37:49

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.

我运行代码,我知道因为警报而调用$ scope.gah(),但是类没有被更新。

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

更新:gah已经从其他地方调用了,我的问题是,当它被调用时,类不会改变

2 个解决方案

#1


2  

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

您只需要使用没有花括号的ng-class指令。

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

please see demo:

请看演示:

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

#2


0  

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

您需要申请ng-class。创建了一个小提琴,帮助您从编码开始

Code snippet:

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

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

UPDATED FIDDLE根据变量字符串设置类

#1


2  

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

您只需要使用没有花括号的ng-class指令。

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

please see demo:

请看演示:

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

#2


0  

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

您需要申请ng-class。创建了一个小提琴,帮助您从编码开始

Code snippet:

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

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

UPDATED FIDDLE根据变量字符串设置类