ProfK ProfK - 1 year ago 77
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:

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:
is only an example I would like to see):

import { Component } from '@angular/core';
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
for the
in my example above?

kit kit
Answer Source

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]'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download