admin管理员组文章数量:1026156
I have a class
.haba-haba{
border:1px solid red;
height:50%;
width:50%;
}
I am storing the class name in scope variable in my controller using
$scope.className = "haba-haba";
How can I assign $scope.className
to my div
as a class?
I tried doing <div ng-class="'{{className:true}}'"></div>
It didn't work.
Anyone can tell me a solution? thanks in advance!
I have a class
.haba-haba{
border:1px solid red;
height:50%;
width:50%;
}
I am storing the class name in scope variable in my controller using
$scope.className = "haba-haba";
How can I assign $scope.className
to my div
as a class?
I tried doing <div ng-class="'{{className:true}}'"></div>
It didn't work.
Anyone can tell me a solution? thanks in advance!
Share Improve this question asked Feb 28, 2014 at 13:08 Proxy_ServerProxy_Server 4012 gold badges7 silver badges18 bronze badges 1-
I don't think that storing
$scope.className = "haba-haba";
is a good idea. Which class to use is view's concern, not model's concern. You should hard-code the classes in view and based on the conditions to apply classes accordingly. – Khanh TO Commented Feb 28, 2014 at 13:41
2 Answers
Reset to default 4The syntax for the ngClass
directive is this:
ng-class="{'haba-haba': thruthValue}"
where
$scope.thruthValue = true;
You can obviously replace true
with false
or a function that returns true
or false
.
Another usage is this:
ng-class="getTheClass()"
where
$scope.getTheClass = function() {
return 'haba-haba';
}
Here's a working fiddle of the second usage.
Also, as other people have pointed out, the second usage is totally against sane MVC. Don't use it unless you know what you are doing.
Actually, what you should do with ng-class
is create a boolean variable and attach it to the ng-class
attribute - when this boolean is true
- the div has that class:
$scope.toggleClass = false;
ng-class="{'haba-haba' : toggleClass}"
Now when you set toggleClass
to true
- your div gets haba-haba
added.
I have a class
.haba-haba{
border:1px solid red;
height:50%;
width:50%;
}
I am storing the class name in scope variable in my controller using
$scope.className = "haba-haba";
How can I assign $scope.className
to my div
as a class?
I tried doing <div ng-class="'{{className:true}}'"></div>
It didn't work.
Anyone can tell me a solution? thanks in advance!
I have a class
.haba-haba{
border:1px solid red;
height:50%;
width:50%;
}
I am storing the class name in scope variable in my controller using
$scope.className = "haba-haba";
How can I assign $scope.className
to my div
as a class?
I tried doing <div ng-class="'{{className:true}}'"></div>
It didn't work.
Anyone can tell me a solution? thanks in advance!
Share Improve this question asked Feb 28, 2014 at 13:08 Proxy_ServerProxy_Server 4012 gold badges7 silver badges18 bronze badges 1-
I don't think that storing
$scope.className = "haba-haba";
is a good idea. Which class to use is view's concern, not model's concern. You should hard-code the classes in view and based on the conditions to apply classes accordingly. – Khanh TO Commented Feb 28, 2014 at 13:41
2 Answers
Reset to default 4The syntax for the ngClass
directive is this:
ng-class="{'haba-haba': thruthValue}"
where
$scope.thruthValue = true;
You can obviously replace true
with false
or a function that returns true
or false
.
Another usage is this:
ng-class="getTheClass()"
where
$scope.getTheClass = function() {
return 'haba-haba';
}
Here's a working fiddle of the second usage.
Also, as other people have pointed out, the second usage is totally against sane MVC. Don't use it unless you know what you are doing.
Actually, what you should do with ng-class
is create a boolean variable and attach it to the ng-class
attribute - when this boolean is true
- the div has that class:
$scope.toggleClass = false;
ng-class="{'haba-haba' : toggleClass}"
Now when you set toggleClass
to true
- your div gets haba-haba
added.
本文标签: javascriptHow to assign class to an element using scope variable in angular jsStack Overflow
版权声明:本文标题:javascript - How to assign class to an element using $scope variable in angular js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745625176a2159818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论