简体   繁体   中英

Angular directive class not being applied

Using element.addClass doesn't add the class onto the element. If I remove class="progress-radial progress-{{percentage}}" from the template it does however work.

Not sure what exactly is going wrong as the code fires correctly, just doesn't seem to want to add a new class if the attribute already exists.

angular.module(moduleName, [])
.directive('npProgress', function() {
  return {
    restrict: 'AE',
    replace: true,
    scope: {
      percentage: '@'
    },
    template: '<div class="progress-radial progress-{{percentage}}">' +
    '<div class="overlay">{{percentage}}</div>' +
    '</div>',
    link: function(scope, element, attrs) {
      if ( scope.percentage > 50 ) {
        element.addClass('progress-radial--positive');
      }
    }
  }
});

Replace the interpolated class with the ng-class directive.

ERRONEOUS

 //AVOID interpolated class template: '<div class="progress-radial progress-{{percentage}}">' + '<div class="overlay">{{percentage}}</div>' + '</div>', 

USE ng-class="'progress-'+percentage" :

template: '<div class="progress-radial" ' +
                'ng-class="' +"'progress-'" + '+percentage">' +
              '<div class="overlay">{{percentage}}</div>' +
          '</div>',

The binding of the interpolated string was fighting the changes by the custom directive.

This is a Known Issue

Known Issues

Dynamically changing an interpolated value

You should avoid dynamically changing the content of an interpolated string (eg attribute value or text node). Your changes are likely to be overwriten, when the original string gets evaluated.

-- AngularJS Developer Guide -- Interpolation -- Known Issues

Try adding the class using angular.element

Here is the Documentation.

Also use parseInt on scope.percentage since you are sending string percentage using @ binding.

parseInt converts the string to integer and then compares.

angular.module(moduleName, [])
.directive('npProgress', function() {
  return {
    restrict: 'AE',
    replace: true,
    scope: {
      percentage: '@'
    },
    template: '<div class="progress-radial progress-{{percentage}}">' +
    '<div class="overlay">{{percentage}}</div>' +
    '</div>',
    link: function(scope, element, attrs) {
      if ( parseInt(scope.percentage) > 50 ) {
        angular.element(element).addClass('progress-radial--positive‌​');

      }
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM