angular2-directives

Angular custom clickaway listener is triggered if a child component is removed using *ngIf

大憨熊 提交于 2020-07-09 11:58:28
问题 I have a clickaway listener as a directive that uses @HostListener put on App.component.ts @Component({ selector: "app-root", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"], }) export class AppComponent { constructor(private clickaway: ClickawayService) {} @HostListener("document:click", ["$event"]) documentClick(event: any): void { this.clickaway.target.next(event.target); } } @Directive({ selector: "[clickaway]", }) export class ClickawayDirective implements OnInit,

Pass parameter to Angular 4 directive on input

筅森魡賤 提交于 2020-06-24 22:20:49
问题 I have an input text field like this <input type="text" class="form-control" [inputTextFilter]="A" [ngModel]="name"> and my directive is: import { Directive, Input, HostListener } from '@angular/core'; @Directive({ selector: '[inputTextFilter]' }) export class InputTextFilterDirective { @Input('inputTextFilter') params: string; @HostListener('keypress', ['$event']) onKeyUp(event: KeyboardEvent) { console.log('got parameters: '+this.params); } } and I have created a Directive called

Angular directive for holderjs - expression evaluation not working as intended

百般思念 提交于 2020-05-16 04:40:48
问题 I am trying to use the excellent holderjs library in an angular2 project. Someone wrote up a very simple attribute directive which I am trying to use. holderjs is basically a client side image placeholder generator. I am trying to modify the directive so that I can pass dynamic placeholders. For example, this works: <img holderjs data-src="holder.js/200x200/auto"> But these don't: <img holderjs data-src="{{myvariable}}"> <img holderjs [data-src]="myvariable"> The angular directive is really a

How to use an EmbeddedViewRef's context variable

谁说胖子不能爱 提交于 2020-05-13 04:49:37
问题 I am a little unsure how to use the EmbeddedViewRef 's context variable. From what I gather from Angular 2's changelog, the context variable replaces the setLocal and getLocal methods as the mechanism for setting local variables in an embedded view. After looking at this blog post, which uses setLocal , I have pieced together the following minimal example: import { Directive, TemplateRef, ViewContainerRef } from '@angular/core' export class FooTemplateContext { constructor(public bar: string,

How to use an EmbeddedViewRef's context variable

醉酒当歌 提交于 2020-05-13 04:49:33
问题 I am a little unsure how to use the EmbeddedViewRef 's context variable. From what I gather from Angular 2's changelog, the context variable replaces the setLocal and getLocal methods as the mechanism for setting local variables in an embedded view. After looking at this blog post, which uses setLocal , I have pieced together the following minimal example: import { Directive, TemplateRef, ViewContainerRef } from '@angular/core' export class FooTemplateContext { constructor(public bar: string,

Detect change in a variable?

守給你的承諾、 提交于 2020-05-12 14:48:31
问题 Is it possible to detect change in a variable? I have the following: @Input('name') name: string; I would like to call a function whenever change is happened in this variable 'name'. Is it possible? 回答1: You can do it the following: private _name = ''; @Input('name') set name(name: string) { this._name = name; doSomeStuff(); } get name(): string { return this._name; } 回答2: I am solve this question using default Angular feature named OnChanges, very similar to OnInit. https://stackoverflow.com

Detect change in a variable?

早过忘川 提交于 2020-05-12 14:48:26
问题 Is it possible to detect change in a variable? I have the following: @Input('name') name: string; I would like to call a function whenever change is happened in this variable 'name'. Is it possible? 回答1: You can do it the following: private _name = ''; @Input('name') set name(name: string) { this._name = name; doSomeStuff(); } get name(): string { return this._name; } 回答2: I am solve this question using default Angular feature named OnChanges, very similar to OnInit. https://stackoverflow.com

How do I dynamically load and display html which contains routerLink directives in Angular 2?

旧时模样 提交于 2020-02-22 06:21:45
问题 I have an Angular 2 component which, on some user action, loads html from a database. The html contains anchor tags with relative paths. Given that I don't want the whole application to reload when the user clicks the link, I am trying to use the routerLink directive; however, since this html is loaded dynamically, the routerLink is not processed. The html will not contain any other angular directives. What is the simplest way to achieve this? See my example below. import { Component } from '

Angular2 : No provider for String! (child -> String) in [Array in parent

怎甘沉沦 提交于 2020-02-02 04:25:32
问题 Im building basic Todo App in angular2 when I click add button to send data from parent to child using input I get No provider for string! (child->string), and no data displayed only button at the child class is shown, what does it mean here is parent component: @Component({ selector : 'parent-component', directives : [child], template : ` <h1 class= "text-center">ToDo App</h1> <div class = "form-group"> <lable>Task</lable> <input type = "text" class = "form-control" #task> <lable>Detail<