Questions tagged [angular2-components]

In Angular 2, Components are the main way we build and specify elements and logic on the page. Questions should include code examples sufficient to reproduce the problem.

Component Documentation:

Declare reusable UI building blocks for an application.

Each Angular component requires a single @Component annotation. The @Component annotation specifies when a component is instantiated, and which properties and hostListeners it binds to.

When a component is instantiated, Angular

creates a shadow DOM for the component. loads the selected template into the shadow DOM. creates all the injectable objects configured with providers and viewProviders. All template expressions and statements are then evaluated against the component instance.

Hello world sample component

import { Component, Input } from '@angular/core';
@Component({
    selector: 'hello-world',
    template: `Hello {{name}}`
})
export class HelloWorldComponent{
    @Input() name: string;
}

Use:

<hello-world [name]="'world'"></hello-world>
508 questions
264
votes
11 answers

How to call another components function in angular2

I have two components as follows and I want to call a function from another component. Both components are included in the third parent component using directive. Component 1: @component( selector:'com1' ) export class com1{ …
noobProgrammer
  • 2,884
  • 3
  • 17
  • 20
224
votes
8 answers

Angular - What is the meanings of module.id in component?

In an Angular app, I have seen that @Component has property moduleId. What does it mean? And when module.id is not defined anywhere, the app still works. How can it still work? @Component({ moduleId: module.id, selector: 'ng-app', …
Nishchit
  • 18,284
  • 12
  • 54
  • 81
95
votes
2 answers

Pass variable to custom component

I have my custom component: @Component({ selector: 'my-custom-component', templateUrl: './my-custom-component.html', styleUrls: ['./my-custom-component.css'] }) export class MyCustomComponent { constructor() { …
rpayanm
  • 6,303
  • 7
  • 26
  • 39
72
votes
3 answers

Angular 2: Component Interaction, optional input parameters

I have an implementation where parent wants to pass certain data to child component via the use of @Input parameter available at the child component. However, this data transfer is a optional thing and the parent may or may not pass it as per the…
Sumit Agarwal
  • 4,091
  • 8
  • 33
  • 49
42
votes
4 answers

Testing ngOnChanges lifecycle hook in Angular 2

Given the following code I try to test the ngOnChanges lifecycle hook of Angular2: import { it, inject, fdescribe, beforeEachProviders, } from '@angular/core/testing'; import {TestComponentBuilder} from…
user1448982
  • 1,200
  • 3
  • 12
  • 22
38
votes
1 answer

What are recommanditions for @Output event names (to prevent native event name collision)?

I have played with Angular 2 components and their compositions and I have run into ugly behavior, which is caused by native event bubbling and @Output name collision. I have component app-form with form in template
milanlempera
  • 2,203
  • 1
  • 17
  • 21
36
votes
2 answers

Angular 2: access an element from the Component, getDocumentById doesn't work

I have an Angular 2 component where I want to retrieve an element div
by its id. I try doing: document.getElementById("myId") in my component (TypeScript), but i get an error: "cannot find name document". I see that in other…
joeCarpenter
  • 1,495
  • 4
  • 19
  • 32
31
votes
3 answers

How to call component method from service? (angular2)

I want to create service, which can interact with one component. All another components in my app, should be able to call this service, and this service should interact with this component. How to call component method from service? @Component({ …
31
votes
3 answers

Can component invoke a self destroy event

I have a parent component which opens a new component on click of a link, this new component is supposed to have a close button which on close sends a closing message to parent and destroy itself. We can send the closing message using ngOnDestroy…
Sumit Agarwal
  • 4,091
  • 8
  • 33
  • 49
29
votes
4 answers

Angular 2: Functions to be used across all components

I have an angular 2 webpack project where I currently have some functions that are repeated throughout several components. I would like to inherit all of these components from a "master" class OR component (whichever works), in order to be able to…
Sina Sohi
  • 2,719
  • 9
  • 33
  • 50
27
votes
4 answers

Accessing `selector` from within an Angular 2 component

I'm trying to figure out how I can access the selector that we pass into the @Component decorator. For example @Component({ selector: 'my-component' }) class MyComponent { constructor() { // I was hoping for something like the following…
26
votes
2 answers

Angular2: Pass by reference to interact between components

When we pass a modelto the child component and it modifies it, the values are just reflected in the child components' local variable and not available to the parent. Can we pass values by reference from parent to child. So the changes are visible…
Sumit Agarwal
  • 4,091
  • 8
  • 33
  • 49
19
votes
3 answers

Share data between components using a service in Angular2

I am developing an application using angular2. I have a scenario where I need to pass complex data (array of objects) from one component to another component(they are not parent-child, they are two separate components) while routing(using…
mrsan22
  • 727
  • 2
  • 11
  • 28
19
votes
1 answer

How to use ResolveComponentFactory() but with a string as a key

What I'm trying to do: Use something similar to the "resolveComponentFactory()", but with a 'string' identifier to get Component Factories. Once obtained, start leverage the "createComponent(Factory)" method. Plnkr Example -> enter link…
Edward
  • 1,076
  • 1
  • 12
  • 24
17
votes
2 answers

Generic type 'Array' requires 1 type argument(s). - Angular2

I have been trying to implement a simple ngFor with Angular2 but I don't know what went wrong which lead to error 'Generic TYpe Array requires one argument(s). PLease favour import { Component } from '@angular/core'; @Component({ …
1
2 3
33 34