angular2-directives

angular2 - passing data object between components

别等时光非礼了梦想. 提交于 2019-12-10 19:34:11
问题 Looking for some help, if possible. I have a data object located in one component, received from a REST call. It contains user details that I would like to share across components, but I am having trouble. What is the best way to pass this object over to another component, so I can use the details? Ex. The data object is named user, and I want to share the user._id created from the database and received from the REST call with a separate component. Thanks everyone for any help. COMPONENT ONE

Extending a Attribute Directive

久未见 提交于 2019-12-10 18:48:00
问题 I would like to extend the built-in routerLink directive to create my own directive called customRouterLink . I have simply created a class that extends RouterLinkWithHref : @Directive({ selector: 'a[customRouterLink]' }) export class CustomLinkDirective extends RouterLinkWithHref { constructor(router: Router, route: ActivatedRoute, locationStrategy: LocationStrategy) { super(router, route, locationStrategy); } @Input() set customRouterLink(data: any[]|string) { console.log("custom directive

How can I call function from directive after component's rendering?

随声附和 提交于 2019-12-10 17:09:25
问题 How can I call function from directive after component's rendering? I have component: export class Component { ngAfterContentInit() { // How can i call functionFromDirective()? } } And I want call this function: export class Directive { functionFromDirective() { //something hapenns } How can i do this? 回答1: You can retrieve Directive from Component's template with ViewChild like this: @Directive({ ..., selector: '[directive]', }) export class DirectiveClass { method() {} } In your component:

Angular2 how to inject parent component into directive only if it's there?

白昼怎懂夜的黑 提交于 2019-12-10 15:20:10
问题 I have a select-all directive for my custom table component. I'd like a user of my directive to be able to instantiate it in two ways: 1: <my-custom-table> <input type="checkbox" my-select-all-directive/> </my-custom-table> 2: <input type="checkbox" [table]="myTableRef" my-select-all-directive/> <my-custom-table #myTableRef></my-custom-table> I was able to get the first way working through the use of Host, Inject, and forwardRef in my directive's constructor: constructor(@Host() @Inject

Are Angular 2 directives now “extensible”?

一曲冷凌霜 提交于 2019-12-10 14:43:15
问题 The biggest problem I have with Angular 1 is how difficult it is to extend (in the object-oriented sense) a directive. For example, it is almost impossible to reuse the input[number] directive on my a custom widget and I had to re-implement all the validation and type conversion code. Angular 2 components are implemented as classes so it seems they can be easily extended. However, they also have that @Component annotation with very specific selectors, etc., which makes it unclear to me if

Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'

浪尽此生 提交于 2019-12-10 14:26:25
问题 I am creating a component then it's set to root component then i am getting error Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. i have using RouterLink as a directive in component anotation but it can't work. app.routes.ts import { ModuleWithProviders } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { dogRoutes } from './Dogs/dog.routes'; import { catRoutes } from './Cats/cat.routes'; import {userRoutes} from ".

Directive display ng-model input value as currency in angular

倾然丶 夕夏残阳落幕 提交于 2019-12-10 12:26:55
问题 I want to display Currency sign with decimal value in my input field. If model value is set to 2 it should display $ 2.00 while model value should remain numeric. I have created a directive that do so but if load value from server where value is set in model then it display $ 2.00 and model value is also string while i need numeric.After googling many posts i have found this soln but it is in angular 1. How it can be converted in angular 2? I need exactly same behavior: Fiddle My directive is

Angular - Google places directive isn't being triggered when the input value changes

喜夏-厌秋 提交于 2019-12-10 11:24:57
问题 I'm trying to write a google place directive since all the finished ones doesn't seem to work very well or have a code base that is just horrendous. So here is my attempt which is clean but it isn't activating when the user types something: @Directive({ selector: '[googlePlace]' }) export class GoogleplaceDirective implements OnInit { @Output() placeChange: EventEmitter<any> = new EventEmitter<any>(); public autocomplete: any; constructor(private elementRef: ElementRef) {} ngOnInit() { this

Extending/decorating Angular 2 components and directives

不羁的心 提交于 2019-12-10 10:39:36
问题 I have some use cases for inheritance and decoration (as in Decorator pattern) of components and directives in Angular 2. The example is component with basic template that doesn't suit the case, to the point when it easier to define a new template instead of modifying the DOM of existing one programmatically. The rest of component metadata should be inherited. Basically it is export const BASE_SOME_COMPONENT_METADATA = { ... }; @Component(BASE_SOME_COMPONENT_METADATA); export class

Using Angular 2 focus on next input field with enter key press

≯℡__Kan透↙ 提交于 2019-12-10 08:17:25
问题 I am new in angular 2. How can I move focus to next control on key enter in Angular 2. I implement this code but how not working properly. Please suggest me how I do this. Thanks My component code import { Component,Injectable,Inject,OnInit, OnDestroy, EventEmitter, Output, Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core'; import {Http, Response} from '@angular/http'; import {TestService} from '../../services/test.service'; import 'rxjs/add/operator/map'; import