Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an angular 2 component be used with an attribute selector?

Tags:

angular

We currently have an existing small Angular 1 project that is used in an on premises Sharepoint 2013 environment. For a large part of our content, we use publishing pages on the Sharepoint environment.

With Angular 1, we could define directives to be restricted to: match attribute name, tag name, comments, or class name. Most of the directives we created were attribute or tag name. The preference would have been tag name, but the publishing platform on Sharepoint strips out unknown elements. So that means we were left with using attributes in order to bring our directives in to the publishing pages. With Angular 2 though, I've only seen components implemented by tag name.

Is it possible with Angular 2 to use attribute names in order to use our components? This is a requirement for us because of the restrictions in the Sharepoint publishing platform.

Thanks.

like image 396
TehOne Avatar asked Jul 20 '16 03:07

TehOne


People also ask

What is the purpose of the selector property when defining an Angular 2 component?

A selector is used to identify each component uniquely into the component tree, and it also defines how the current component is represented in the HTML DOM. When we create a new component using Angular CLI, the newly created component looks like this.

Can Angular component have multiple selectors?

You can't use id or selectors that span multiple elements.

What is used to select a specific component in Angular?

Specifying a component's CSS selectorlink A selector instructs Angular to instantiate this component wherever it finds the corresponding tag in template HTML. For example, consider a component hello-world.

What does the Angular @component decorator's selector property define?

Decorator that marks a class as an Angular component and provides configuration metadata that determines how the component should be processed, instantiated, and used at runtime.


1 Answers

Yes, the selector property of the @Component decorator is a CSS selector (or a subset of):

selector: '.cool-button:not(a)'

Specifies a CSS selector that identifies this directive within a template. Supported selectors include element, [attribute], .class, and :not().
Does not support parent-child relationship selectors.

Source: Angular Cheat Sheet / Directive Configuration, which @Component inherits.

That way you can use [name-of-the-attribute] (namely, the CSS attribute selector), such as:

@Component({     selector: "[other-attr]",     ... }) export class OtherAttrComponent { 

Se demo plunker here.

The usual way is the CSS type (AKA element or tag) selector:

@Component({     selector: "some-tag",     ... }) 

And it matches a tag with name some-tag.

You can even have a component that matches both a tag or an attribute:

@Component({     selector: "other-both,[other-both]",     template: `this is other-both ({{ value }})` }) export class OtherBothComponent { 

Demo plunker contains examples of all three.

Is [attributeName="attributeValue"] supported?

Yes. But mind the quotes. In the current implementation, the selector [attributeName="attributeValue"] actually matches <sometag attributeName='"attributeValue"'>, so test around before committing to this approach.

like image 70
acdcjunior Avatar answered Oct 24 '22 17:10

acdcjunior