angular-directive

Input field accepting only numbers without firing events when input other caracters

余生长醉 提交于 2019-12-05 09:31:02
问题 I'm not satisfied with the default behavior of the html number input, I would like to have a simple input that accepts only numbers. I created this directive : import { Directive, ElementRef, HostListener, Input } from '@angular/core'; import { NgControl } from '@angular/forms'; @Directive({ selector: 'input[numbersOnly]' }) export class NumberDirective { constructor(private _el: ElementRef) { } @HostListener('input', ['$event']) onInputChange(event) { const initalValue = this._el

Angular - Apply style to element depending on sibling RouterLinkActive?

孤者浪人 提交于 2019-12-05 09:16:47
I do not have only one menu bar on my app that I need to be painted when the user navigates, I have another components too that needs to be painted as well. Can I achieve this just using routerLinkActive ? menu.html <menu> <a routerLink="page1" routerLinkActive="active"> option1 </a> <a routerLink="page2" routerLinkActive="active"> option2 </a> </menu> This menu works great. But what I'm asking is how can I take advantage of routerLinkActive property placed in other HTML Tag. like: main.html <main> <span class="title" routerLinkActive="active">My sub child part</span> </main> You could bind

Angularjs form validation order

孤者浪人 提交于 2019-12-05 01:32:11
问题 I have a simple html form containing regular text input. ng-minlength , ng-maxlength and ng-pattern angular built-in form input directives are set on the input. Problem: ng-pattern check is applied before the length check by ng-minlength and ng-maxlength . Question: how can I change the default check order: i.e. first check for the length, then apply pattern check? Example: <body ng-app> <div> <form name="myForm"> Name: <input name="name" type="text" ng-model="name" ng-minlength="3" ng

Angular 4 call directive method from component

冷暖自知 提交于 2019-12-05 00:49:52
i'm trying to build a structural directive that will change the parent DOM structure either invoked by using its selector (static) or by calling its public method(dynamic). Using the directive selector in a html template works fine without any issues. I'm trying to figure out if we an achieve the same without using it in template and by calling the directive method. my-directive.ts @Directive({ selector: '[sampleDirective]' }) export class SampleDirective { ... constructor(..) { this.customDirective(); } } customDirective() { console.log('Inside customDirective()'); } my-component.ts import {

How to revalidate a form with multiple dependent fields?

半世苍凉 提交于 2019-12-04 20:30:05
I'm fairly new to Angular. I have a form where the user need to assign port numbers to 9 different port input fields (context: it's a form for a server environment configuration). The validation requirement is that no port number can be assigned twice, so each of the 9 port numbers needs to be unique. For that, I have a custom validation directive called "srb-unique-port", which I assign to my input fields. Directive: (function () { 'use strict'; angular .module('account') .directive('srbUniquePort', [srbUniquePort]); function srbUniquePort() { return { restrict: 'A', require: 'ngModel', scope

Angular 4 Template-Forms multi-field validation directive and ngModelGroup

浪尽此生 提交于 2019-12-04 19:48:01
I am trying to validate a typical new password and confirm password match using an Angular Directive . The error message is not shown even though the directive correctly catches a mis-match: Template Form <form> <md-input-container class="signup-full-width"> <input mdInput autocomplete="off" type="password" name="password" #currentPasswd="ngModel" placeholder="Current password" [(ngModel)]="currentPassword" required> <md-error *ngIf="currentPasswd.errors && (currentPasswd.dirty || currentPasswd.touched)" [ngStyle]="{'color': 'red'}"> <div [hidden]="!currentPasswd.errors.required">Required</div

Angular get nested element using directive on parent

巧了我就是萌 提交于 2019-12-04 18:40:42
Here is the code that I have <parent my-directive [toHide]="childRef"> <child bottom right #childRef> <button>Some text </button> </child > </parent > Here I have my-directive on the parent element and I want to access the child and apply some style to it. So in my directive, I have the following. export class MyDirective { @Input("toHide") localRef; constructor(public element:ElementRef,public renderer:Renderer) { console.log('Hello MyDirective Directive'); } ngAfterViewInit() { console.log("All Transtition set"); console.log(this.localRef); this.renderer.setElementStyle(this.localRef,

How can I make a structural directive to wrap part of my DOM?

旧城冷巷雨未停 提交于 2019-12-04 17:37:11
I currently have the following line in my HTML: <p> this is my first line </p> Using a wrapper directive I want to add a second paragraph and wrap it in a div so it will look like this: <p wrapper> this is my first line </p> And then the directive will add the wrapper and second line to make the final HTML look like this: <div> <p> this is my first line </p> <p> this is my second </p> </div> From what I understand from angular.io I will need to create a structural directive and use a TemplateRef and a ViewContainerRef, but I can't find an example on how to use them to wrap an existing part of

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

て烟熏妆下的殇ゞ 提交于 2019-12-04 17:24:43
I have looked at the questions regarding this error, and have not found a solution. I have a highlight directive, and I take an input index . This directive works when I declare it in the module I'm using it in. But I want to use it in several modules, So I removed the declaration, and put it inside of my root module that imports the errors. That is when I get the error: Error: Template parse errors: Can't bind to 'index' since it isn't a known property of 'div'. (" <div class="read row" appListHighlight [ERROR ->][index]="index" > <div class="col"></div> "): ng:///NetworkModule/DnsComponent

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=