0

I'm trying to code a component selector on attribute value like this:

selector: '[data-type=Type1]'

This works great in the following HTML:

<div *ngFor="let item of items">
  <div data-type="Type1"></div>
</div>

But does not work like this (which is of course what I want):

<div *ngFor="let item of items">
  <div [data-type]="item.type"></div>
</div>

I'm guessing it is due to the order in which Angular resolves things. Is what I want possible?

3 Answers 3

3
+50

Directives cannot be added or removed based on change detection. Either the directive is there always or it is not.

Your directive should take as an input the data-type value and then determine whether to perform its job or not.

E.g.

@Directive({
  selector: '[data-type]'
})
export class DataTypeDirective {
  @Input('data-type') dataType: string;

  doSomeStuff() {
    if (this.dataType !== 'Type1') {
      return;
    }
    // perform the directive's function
  }
}

Or in case it's a component, use an *ngIf in the template:

<div *ngIf="dataType === 'Type1'">
  <!-- the component's body -->
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

It's a component, and I want to be able to have a lot of them, add more when needed and not have to update all the HTML where they are used with increasing numbers of ngIf's. And what I'm reading in your answer is what seemed to be the case when I posted the question: just not possible.
You don't have to update the templates where they are used, just the template of the component itself, just one time.
I see what you're saying. That's not as bad.
0

I think there's a slight problem in syntax maybe. Try using '[selector_name]'. Adding a link, this one for use of selector.

3 Comments

I don't follow you. What do you think needs to change, specifically?
In selector -> selector: '[selector_value]' In Component -> @Component({ selector: '[my-app]', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) In HTML -> <body> <div my-app> </div> </body>
So the upper one is the correct format of taking selector as an attribute. That you've already done! <div data-type="Type1"></div>
0

For attribute binding syntax resembles property binding, but instead of an element property between brackets, you precede the name of the attribute with the prefix attr, followed by a dot. Then, you set the attribute value with an expression that resolves to a string. https://angular.io/guide/attribute-binding

in your case, should be in this way

<div *ngFor="let item of items">
  <div [attr.data-type]="item.type"></div>
</div>

3 Comments

I'm afraid that doesn't seem to work either. Not getting the component matched to the div.
should be worked, for me works here stackblitz.com/edit/…
In your stack blitz I do not see where you have indicated an attribute value based selector for any component. Please see the first code block in my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.