简体   繁体   中英

Conditional ngClass inside a custom directive

I am trying to learn about isolated scope.

Lets say, I have a simple directive:

HTML:

<my-directive options = "data"></my-directive>

JS:

angular.module('myapp.directive').
    directive('myDirective', function() {
     return {
        template: '<a href = "{{href}}" class = "class"></a>',
         restrict: 'E',
         replace: 'true',
         scope: {
           options = "=options"
         },
         link: function(scope, element, attrs) {
            scope.$watch('options', function(data) {
              scope.class = data.class;
              scope.href = data.href;
            }
         }

    }

It works. Now I want to add ng-class:

<my-directive ng-class = "{'enabled': data.status == 'enabled'}" options = "data"></my-directive>

I tried:

scope : {
   options: '=options',
   ngClass: "="
}

scope.$watch("ngClass", function(value) {
   scope.class += value;
}

It puts "{'enabled': data.status == 'enabled'}" into class. Do i need to compile it or how do i make it to eval ng class everytime data is updated?

On browser I see

<a href = "{{href}}" class = "class "{'enabled': data.status == 'enabled'}""></a>

I want to see

<a href = "{{href}}" class = "class enabled"></a>

Pass ng-class from myDirective to your template using a template function:

<my-directive ng-class = "{'enabled': data.status == 'enabled'}" options = "data">
</my-directive>

Directive

angular.module('myapp.directive').
directive('myDirective', function() {
 return {
    template: function(element, attr) { 
         return '<a href = "{{href}}" class = "class" ng-class="' + attr.ngClass + '"></a>'
     },
     restrict: 'E',
     replace: 'true',
     scope: {
       options = "=options"
     },
     link: function(scope, element, attrs) {
        scope.$watch('options', function(data) {
          scope.class = data.class;
          scope.href = data.href;
        }
     }

}

Alternatively, you can use $eval to convert the ngClass expression to an object and then bind it to a scope variable within your directive's isolated scope so that it can be used within your template:

HTML

<my-directive ng-class = "{'enabled': data.status == 'enabled'}" options = "data">
</my-directive>

Directive

angular.module('myapp.directive').
directive('myDirective', function() {
 return {
    template: function(element, attr) { 
         return '<a href = "{{href}}" class = "class" ng-class="modelClass"></a>'
     },
     controller: function($scope, $element, $attrs) {
        $scope.modelClass = $scope.$eval($attrs.ngClass);
     },
     restrict: 'E',
     replace: 'true',
     scope: {
       options = "=options"
     },
     link: function(scope, element, attrs) {

        scope.$watch('options', function(data) {
          scope.class = data.class;
          scope.href = data.href;
        }
     }

}

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