Angular 2 conditionally add attribute directives

15,139

Solution 1

That is not supported. Directives are only applied when static HTML matches the selector.

Solution 2

I know this is a late reply, but might help someone.

You can pass a condition as an input to the attribute directive,

<h1 colored [enable]="check">Testing something</h1>

and then in the directive

    import {Directive, ElementRef} from '@angular/core'

@Directive({
    selector: '[colored]',
    host: {
        '(mouseenter)': 'onMouseEnter()',
        '(mouseleave)': 'onMouseLeave()'
    }
})
        
export class colorDirective {
    
    @Input() enable: boolean = true;

    constructor(private el: ElementRef) {
    }
    onMouseEnter() { 
        if(this.enable){
            this.highlight("yellow");  
        }
    }   

    onMouseLeave() { 
        if(this.enable){
            this.highlight(null); 
        }
    }

    private highlight(color: string) {
        this.el.nativeElement.style.backgroundColor = color;
    }
}
        

 
Share:
15,139

Related videos on Youtube

Denko Mancheski
Author by

Denko Mancheski

Updated on June 14, 2022

Comments

  • Denko Mancheski
    Denko Mancheski almost 2 years

    I've seen two questions here how to conditionally add and remove attributes on an item (Is it possible to conditionally display element attributes using Angular2?) but my question is if it is possible to add and remove attribute directives ? I am able to add and remove the attribute but Angular does not "compile" the attribute as an attribute directive but the attribute just sits there doing nothing. Here is an example of 2 tags:

    The first one is the one that I am trying to conditionally apply the attribute directive and the second one has it all the time.

    Here is the gif: enter image description here

    Here is how I am applying the attribute (maybe there is a different way to apply attribute directive?)

    <h1 [attr.colored]="check ? '': null">Testing something</h1>
    

    And here is the directive:

    import {Directive, ElementRef} from '@angular/core'
    @Directive({
        selector: '[colored]',
        host: {
            '(mouseenter)': 'onMouseEnter()',
            '(mouseleave)': 'onMouseLeave()'
        }
    })
    
    export class colorDirective {
        constructor(private el: ElementRef) {
        }
        onMouseEnter() { this.highlight("yellow"); }
        onMouseLeave() { this.highlight(null); }
    
        private highlight(color: string) {
            this.el.nativeElement.style.backgroundColor = color;
        }
    }
    

    Edit: There are couple answers but they are for AngularJS (1)

  • Denko Mancheski
    Denko Mancheski almost 8 years
    Is there any way how to force angular to recompile that element somehow to apply it ? or is there any different way how to do this? I dont need it right now but I was just curious
  • Günter Zöchbauer
    Günter Zöchbauer almost 8 years
    Not as far as I am aware of. What you can do is to add one element with and one without the directive selector and use ngIf to switch which one should be added/removed from the DOM.
  • Abderrahim
    Abderrahim almost 8 years
    @GünterZöchbauer can you please take a look at my question maybe you have an idea stackoverflow.com/questions/37489029/…
  • Günter Zöchbauer
    Günter Zöchbauer almost 8 years
    Sorry, I don't know AngularJS, only Angular2
  • Travesty3
    Travesty3 over 7 years
    Here's my use case: I'm using Bootstrap. I created a component for text inputs so that I don't have to create the wrapper div (.form-group) and the label for the text input, and can instead just use one line for each text input, passing the label as an @Input() to the component. I have also created a CurrencyDirective to format the input as currency. Now I want to be able to set that directive on one of my text inputs, but I would have to pass a flag into the text input component that indicates whether or not the directive should be added to the <input /> element in the component.
  • Zama Mohammed
    Zama Mohammed over 7 years
    @GünterZöchbauer whats the reasoning of not supporting such important functionality. ngIf doesn't sounds a solution to me. :(
  • Günter Zöchbauer
    Günter Zöchbauer over 7 years
    I don't know about the reasoning. Never saw any response about this topic.