Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2—change component selector

The Angular 2 docs say to define a component like this, using a magic string for the selector name:

import { Component } from '@angular/core';
@Component({
   selector: 'card',
   template: `
     <div class="card">{{title}}</card>
   `
})
export class Card {
   title = 'Card Title';
}

If I do this, am I stuck with <card> as the only way to use this component in the future? Can an app have only one type of component with this selector? For example, is it possible to use a <card> from a third-party library, and also use my own <card>?

This is trivial in React, because component names are just variables.

import OtherCard from 'card'
const Card = title => <div className="card">{title}</div>
const Composable = title => <OtherCard><Card title={title} /></OtherCard>

One of the reasons I ask is so I can know whether to namespace Angular 2 component selectors like in Angular 1 and Objective-C:

selector: 'initech-card'

like image 884
Max Heiber Avatar asked Sep 16 '16 13:09

Max Heiber


People also ask

Can two components have same selector?

Two or more components use the same element selector. Because there can only be a single component associated with an element, selectors must be unique strings to prevent ambiguity for Angular.

How can we use selector of one component in another?

To use selector as directive, change is required in the selector of the child component. Selector name need to be closed in square bracket, just like in case of property binding. Now the child component can be included in the parent using directive like this.

What is selector in component Angular?

What is a Selector in Angular? A selector is one of the properties of the object that we use along with the component configuration. 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.


1 Answers

You should namespace Angular2 component selectors (but not with ng- ;-) )

I think you can't import 2 modules that contain elements with the same selector (not tried).

This limits you to one component with a specific selector within one module. If you encounter a collision you might be able to split your code in 2 modules where one module imports <card> from ModuleA and the other module imports <card> from ModuleB.

I wouldn't expect such collisions to happen a lot.

EDIT (updating answer from comments)

Components are module-scoped, which gives you the ability to use components with third-party components with the same selector, like in the Composable React component in the example in the question.

To use a <card> you wrote with a third-party <card>, you can wrap the third-party component with another component that is part of an imported module and then use the wrapper component instead.

update

For changing the selector of the root component see also

  • https://github.com/angular/angular/pull/14750
  • https://github.com/angular/angular/issues/7136
  • https://github.com/angular/angular/issues/13035
like image 194
Günter Zöchbauer Avatar answered Sep 20 '22 10:09

Günter Zöchbauer