从AngularJS更改CSS [英] Changing CSS from AngularJS
问题描述
从DOM变化angularjs控制器是不是一个好的做法。在我的应用程序,点击一个链接后,我改变类内部ngView HTML元素。预期的行为是,我有三个div的,并且如果显示与否中间的一个,我改变。我从控制器这样做。我已阅读,这样做的DOM操作应该以指令来完成,但是我的心是不够宽泛,不能找到一个解决方案。请,如果你有一个建议,我会很高兴。
DOM changes from angularjs controllers are not a good practice. In my application, after clicking on a link, I am changing class of an html element inside ngView. the intended behaviour is, that i have three divs, and I am changing if the middle one is shown or not. I am doing this from a controller. I have read, that doing DOM manipulation should be done in a directive, but my mind is not broad enough to find a solution. Please, if you have a suggestion, I will be glad.
推荐答案
使用 NG-类。
例如:
http://jsfiddle.net/rd13/eTTZj/75/
app = angular.module('myApp', []);
app.directive("click", function () {
return function(scope, element, attrs) {
element.bind("click", function() {
scope.boolChangeClass = !scope.boolChangeClass;
scope.$apply();
});
};
});
一些HTML代码:
Some HTML:
<div id="page">
<div>One</div>
<div ng-class="{'my-class':boolChangeClass}">Two</div>
<div>Three</div>
<button click>Click me</button>
</div>
当您单击该按钮,类中心的div会根据你的范围内设置boolen值更改。
When you click the button, the class of the center div will change depending on the boolen value set within your scope.
这篇关于从AngularJS更改CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!