最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get and Set class of an element in AngularJs - Stack Overflow

programmeradmin0浏览0评论

I've added the ng-click to my icon like this:

<img src="images/customer_icon.png" class="customer" ng-click="processForm($event)">

And here's the js-file.

   var app = angular.module('myapp', []);
    app.controller('mycontroller', function($scope) {
        $scope.processForm = function(obj) {
            var elem = angular.element(obj);
            alert("Current class value: " + elem.getAttribute("class"));
       };
    });

I'm getting the "elem.getAttribute is not a function" error. Trying to switch "class" to "ng-class", i faced another problem: "ng-class" is not recognised by my css files.

Is there any way I can get/set the "class" attribute directly?

I've added the ng-click to my icon like this:

<img src="images/customer_icon.png" class="customer" ng-click="processForm($event)">

And here's the js-file.

   var app = angular.module('myapp', []);
    app.controller('mycontroller', function($scope) {
        $scope.processForm = function(obj) {
            var elem = angular.element(obj);
            alert("Current class value: " + elem.getAttribute("class"));
       };
    });

I'm getting the "elem.getAttribute is not a function" error. Trying to switch "class" to "ng-class", i faced another problem: "ng-class" is not recognised by my css files.

Is there any way I can get/set the "class" attribute directly?

Share Improve this question edited Sep 24, 2016 at 12:01 Mohit Tanwani 6,6382 gold badges15 silver badges33 bronze badges asked Sep 9, 2016 at 9:13 Chernogorskiy FedorChernogorskiy Fedor 951 gold badge2 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Try following snippet.

var app = angular.module('myapp', []);
app.controller('mycontroller', function($scope) {
    $scope.processForm = function(event) {
      var element = angular.element(event.target);
      //Old Class
      console.log("OLD CLASS : " + element.attr('class'));
      element.removeClass('customer').addClass("newClass");
      //New Class
      console.log("NEW CLASS : " + element.attr('class'));
    };
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="mycontroller">
<img src="http://placehold.it/200x100" class="customer" ng-click="processForm($event)">
</div>

More information angular.element

You can use the classList property which is native and can help you a lot :

div.classList.remove("foo");
div.classList.add("anotherclass");
div.classList.toggle("visible");
console.log(div.classList.contains("foo"));

Try this below code instead of yours

var elem = obj.target.attributes.class.value;

Also Why ng-class is not working for you? It's should be working, I think you may be mismatched your CSS file.

elem is an array, so you have to use elem[0]:

$scope.processForm = function(obj) {
   var elem = angular.element(obj.currentTarget);// or angular.element(obj.target);
   alert("Current class value: " + elem[0].getAttribute("class"));
};
发布评论

评论列表(0)

  1. 暂无评论