angular2-components

Communicate between sibling components Angular 2

醉酒当歌 提交于 2019-11-29 01:13:45
问题 I tried to follow this answer but its too confusing Angular 2 event catching between sibling components I want to call a method in child component 1 when something is clicked on child component 2 Child component 2 emits an event called trackClick. Parent Component: <div> <audio-player></audio-player> <audio-albums></audio-albums> </div> Child Component 1 (audio-player) // Don't know what to do here, want to call this function trackChanged(track){ console.log("YES!! " + track); } Child

Pass variable to custom component

无人久伴 提交于 2019-11-28 11:52:59
I have my custom component: @Component({ selector: 'my-custom-component', templateUrl: './my-custom-component.html', styleUrls: ['./my-custom-component.css'] }) export class MyCustomComponent { constructor() { console.log('myCustomComponent'); } } I can use it like this: <my-custom-component></my-custom-component> But how I can pass a variable? For example: <my-custom-component custom-title="My Title"></my-custom-component> And use this in my component code? You need to add Input property to your component and then use property binding to pass value to it: import { Component, Input } from '

How to call component method from service? (angular2)

天涯浪子 提交于 2019-11-28 06:00:33
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({ selector:'component' }) export class Component{ function2(){ // How call it? } } From this servive? @Injectable() export class Service { callComponentsMethod() { //From this place?; } } Interaction between components can be indeed achieved using services. You will need to inject the service use for inter-component communication into all the components

Share data between components using a service in Angular2

◇◆丶佛笑我妖孽 提交于 2019-11-27 13:15:10
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 router.navigate()). I have googled this and most of the results describe the scenario of components that are parent-child. I have gone through the results and found these ways to pass the data. 1) Create Shared Service 2) Pass as route parameters 2nd approach works for me (even though, I don't like that when I have complex data as explained above). I am not

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

断了今生、忘了曾经 提交于 2019-11-27 09:16:25
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 description here In the example, you will see the "AddItem" method addItem(componentName:string):void{ let compFactory: ComponentFactory; switch(componentName){ case "image": compFactory = this.compFactoryResolver.resolveComponentFactory(PictureBoxWidget); break; case "text": compFactory = this.compFactoryResolver.resolveComponentFactory(TextBoxWidget); break;

How to pass an expression to a component as an input in Angular2?

会有一股神秘感。 提交于 2019-11-27 07:54:50
I need to pass an expression to a component that will be evaluated inside an component's template. For example, component: @Component({ selector: 'app-my-component', ... }) export class MyComponent { @Input items: MyClass; @Input expression: String; ... } with component's template: <div *ngFor="let item of items"> {{expression}} </div> Usage of MyComponent: <app-my-component [items]="listOfItems" [expression]="'[item.id] item.name'"> </app-my-component> As there will be more than one input, I would like to avoid usage of TemplateRef. Maybe one of this options can helps you: 1) Using

angular2 pass ngModel to a child component

余生颓废 提交于 2019-11-27 05:58:37
问题 I have ParentComponent and a ChildComponent, and I need to pass the ngModel in ParentComponent to ChildComponent. // the below is in ParentComponent template <child-component [(ngModel)]="valueInParentComponent"></child-component> how can I get the value of ngModel in ChildComponent and manipulate it? 回答1: You need to implement ControlValueAccessor in the child class. It's what defines your component as "having a value" that can be bound to using the angular way. Read more about it here: http

Pass variable to custom component

六月ゝ 毕业季﹏ 提交于 2019-11-27 03:02:43
问题 I have my custom component: @Component({ selector: 'my-custom-component', templateUrl: './my-custom-component.html', styleUrls: ['./my-custom-component.css'] }) export class MyCustomComponent { constructor() { console.log('myCustomComponent'); } } I can use it like this: <my-custom-component></my-custom-component> But how I can pass a variable? For example: <my-custom-component custom-title="My Title"></my-custom-component> And use this in my component code? 回答1: You need to add Input

ng2 - dynamically creating a component based on a template

大城市里の小女人 提交于 2019-11-27 01:13:49
I've been looking at the Angular 2 APIs for ComponentResolver and DynamicComponentResolver for creating dynamic components, but I have something different in mind than those APIs offer. Is there any way in NG2 to create a component based on a string of its class name? For example, Im building a configurable charts dashboard. Each user's layout is stored in the database, stating they want 2x line charts here, 3x bar charts there, etc. When I load this data as json it looks something like: user.charts = [ { type: 'LineChartComponent', position: ... } { type: 'BarChartComponent', position: ... }

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

半腔热情 提交于 2019-11-26 18:21:45
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', templateUrl: 'app.component.html', styleUrls: ['app.component.css'], directives: [AppComponent] }); Nishchit Dhanani The beta release of Angular (since vesion 2-alpha.51) supports relative assets for components, like templateUrl and styleUrls in the @Component decorator. module.id works when using CommonJS. You don't need to worry about how it works. Remember :