angular2-directives

directive click outside angular 6

拈花ヽ惹草 提交于 2019-12-05 03:00:07
I upgraded my Angular from 4 to 6, and consequently had a problem with my click-off policy, it stopped working on all components. my directive: import { Directive, Output, EventEmitter, ElementRef, HostListener } from '@angular/core'; @Directive({ selector: '[clickOutside]' }) export class ClickOutsideDirective { constructor(private _elementRef : ElementRef) { } @Output() public clickOutside = new EventEmitter(); @HostListener('document:click', ['$event.target']) public onClick(targetElement) { const clickedInside = this._elementRef.nativeElement.contains(targetElement); if (!clickedInside) {

Angular 2 passing parameters to constructor throws DI exception

北慕城南 提交于 2019-12-05 02:13:01
I'm wanting to setup a string property on a component in my constructor, but when I try something like this @Component({ selector: 'wg-app', templateUrl: 'templates/html/wg-app.html' }) export class AppComponent { constructor(private state:string = 'joining'){ } } I get a DI Exception EXCEPTION: No provider for String! (AppComponent -> String) Clearly, the injector is trying to find a 'string' provider, and can't find any. What sort of pattern should I be using for this type of thing? Eg. passing initial parameters to a component. Should it be avoided? Should I be Injecting the initial string?

Angular 4 Can't bind to <property> since it isn't a known property of <component>

[亡魂溺海] 提交于 2019-12-04 22:59:40
I'm trying to create my own directive in Angular 4. But, I got this error when bind the property of class into component template. Console error: Unhandled Promise rejection: Template parse errors: Can't bind to 'data' since it isn't a known property of 'tree'. ("<tree [ERROR ->][data]="data"></tree>"): My tree-view-component.ts: @Component({ selector: 'app-tree-view', template: '<tree [data]="data"></tree>' }) export class TreeViewComponent implements OnInit { @Input() data: any[]; constructor() { this.data = [ { label: 'a1', subs: [ { label: 'a11', subs: [ { label: 'a111', subs: [ { label:

Angular 2 : HTML property binding

冷暖自知 提交于 2019-12-04 20:15:15
I'm trying to understand the HTML bindings as I'm new to angular. Can someone please explain the difference between the following syntax: <!-- 1 --> <button name1 = "name2" >test</button> <!-- 2 --> <button (name1) = "name2" >test</button> <!-- 3 --> <button [name1] = "name2" >test</button> <!-- 4 --> <button ([name1]) = "name2" >test</button> I have seen above in multiple places but could not understand the purpose of each case. Thank you for the help! There are two different thinks.. bindings and events: Here's a live-demo: https://plnkr.co/edit/gfJL9RCyYriqzP9zWFSk?p=preview Binding binds

Angular 2 Cursor jump at the end of textbox when changing data

一笑奈何 提交于 2019-12-04 19:27:25
问题 There is some restrictions in name field so I am trying to validate name field using directive as below. Inside directive I am using regular expression to check valid name and then replacing valid name into textbox using valueAccessor.writeValue(newVal) Here issue is when I am trying to type middle of some word in textbox cursor jump at the end. @Directive({ selector: '[validateName]', host: { '(ngModelChange)': 'onInputChange($event, false)', '(keydown.backspace)': 'onInputChange($event

Globally register a directive in Angular

若如初见. 提交于 2019-12-04 16:05:46
问题 I am developing an Angular application. I need to add special behavior to all links. In AngularJS would just write a directive like this: angular.module('whatever.module', []).directive('href', function() { return { restrict: 'A', link: function($scope, $element, $attrs) { // do stuff } }; }); In Angular I can write a directive like this: @Directive({ selector: '[href]', }) export class MyHrefDirective { constructor() { // whatever } } But how can I tell the application to use that directive

Any angular2 image crop directive available?

自古美人都是妖i 提交于 2019-12-04 15:35:48
Is there any angular2 image crop directive like ngImgCrop Yes we can use ng2-img-cropper component for the same, you just have to install the package named as ng2-img-cropper from the node using npm install ng2-img-cropper --save than just use the component by importing import {ImageCropperComponent, CropperSettings, Bounds} from 'ng2-img- cropper'; Working plunker here For more info see here, https://www.npmjs.com/package/ng2-img-cropper 来源: https://stackoverflow.com/questions/38149250/any-angular2-image-crop-directive-available

Compile Angular 2 node_modules files into one file

拥有回忆 提交于 2019-12-04 15:08:06
I am trying to figure out a way to compile everything I need from the angular 2 node_modules folder into one file.. It seems that there is a crazy amount of http calls when my site is loading and surely thats not optimal or the recommended process of the new and improved angular.. Im not concerned with my own typescript files as i know how to handle them but what are your approaches to concatenating the node_modules files etc ? If you're using SystemJS 1. package.json : add these to devDependencies "devDependencies": { "gulp": "^3.9.1", "systemjs-builder": "^0.15.16" } 2. do npm install 3.

How to restrict Special character in material input

帅比萌擦擦* 提交于 2019-12-04 12:01:41
问题 I have a material input control, i have restrict the special character while user enter, but when type some words in any editor and copy and paste the words with special character, which is not working. I have write the directive for that to prevent special character,but can one provide the better solution restrict in copy paste. app.component.html: <form class="example-form"> <mat-form-field class="example-full-width"> <input matInput specialIsAlphaNumeric placeholder="Favorite food" value=

How to load image with spinner in angular2

こ雲淡風輕ζ 提交于 2019-12-04 10:36:21
问题 my app got many images with descriptions. When user navigates these text is coming first and image is loading with some delay. I would like to add a spinner here. A directive which shows spinner while loading the image and shows image like <myimgdir [src]='myimage.png'></myimgdir> How to add spinner and track the image is loaded or not and display it? 回答1: In your controller, add a function to to handle the 'onLoad' event, setting the state to {loading: false} : loading: boolean = true onLoad