ProfK ProfK - 2 months ago 12
AngularJS Question

Does an Angular 2 '@Component' decorator always need an element name selector?

In this example, from the official Angular 2 docs, the decorator looks like this:

@Component({
selector: 'my-app',
template: '<h1>My First Angular App</h1>'
})


Example: would prefer not not have my HTML code littered with non-standard elements, and would prefer something like (NB:
ng-angular
is only an example I would like to see):

import { Component } from '@angular/core';
@Component({
template: '<h1>Wait! Bloody wait some more.</h1>'
})
export class ListComponent { }


and used something like this:

<div ng-component="List"</div>


Or is the a Component decorator like this used only when you want to create a new HTML element, and then stick to a plain
Listcontroller
for the
div
in my example above?

kit kit
Answer

A selector is not always needed eg. you have a top component of a module that is loaded by router and displayed in

selector is needed for any other type of component. otherwise angular wouldn't know what component it should render.

I haven't heard about attribute "ng-component"

[EDIT] kit effectively answered correctly in his/her first comment:

You have to create an element that would enclose your template however it doesn't have to be a new HTML element because selector can be a element, [attribute] or class, eg.

<div test>

could be an element for component with selector: '[test]'

Comments