How to use onBlur event on Angular2?

前端 未结 7 1222
时光取名叫无心
时光取名叫无心 2020-12-02 21:36

How do you detect an onBlur event in Angular2? I want to use it with


Can anyone help me understand how to use i

相关标签:
7条回答
  • 2020-12-02 22:09
    /*for reich text editor */
      public options: Object = {
        charCounterCount: true,
        height: 300,
        inlineMode: false,
        toolbarFixed: false,
        fontFamilySelection: true,
        fontSizeSelection: true,
        paragraphFormatSelection: true,
    
        events: {
          'froalaEditor.blur': (e, editor) => { this.handleContentChange(editor.html.get()); }}
    
    0 讨论(0)
  • Use (eventName) for while binding event to DOM, basically () is used for event binding. Also use ngModel to get two way binding for myModel variable.

    Markup

    <input type="text" [(ngModel)]="myModel" (blur)="onBlurMethod()">
    

    Code

    export class AppComponent { 
      myModel: any;
      constructor(){
        this.myModel = '123';
      }
      onBlurMethod(){
       alert(this.myModel) 
      }
    }
    

    Demo

    Alternative(not preferable)

    <input type="text" #input (blur)="onBlurMethod($event.target.value)">
    

    Demo


    For model driven form to fire validation on blur, you could pass updateOn parameter.

    ctrl = new FormControl('', {
       updateOn: 'blur', //default will be change
       validators: [Validators.required]
    }); 
    

    Design Docs

    0 讨论(0)
  • 2020-12-02 22:21

    You can also use (focusout) event:

    Use (eventName) for while binding event to DOM, basically () is used for event binding. Also you can use ngModel to get two way binding for your model. With the help of ngModel you can manipulate model variable value inside your component.

    Do this in HTML file

    <input type="text" [(ngModel)]="model" (focusout)="someMethodWithFocusOutEvent($event)">
    

    And in your (component) .ts file

    export class AppComponent { 
     model: any;
     constructor(){ }
     someMethodWithFocusOutEvent(){
       console.log('Your method called');
       // Do something here
     }
    }
    
    0 讨论(0)
  • 2020-12-02 22:21

    HTML

    <input name="email" placeholder="Email"  (blur)="$event.target.value=removeSpaces($event.target.value)" value="">
    

    TS

    removeSpaces(string) {
     let splitStr = string.split(' ').join('');
      return splitStr;
    }
    
    0 讨论(0)
  • 2020-12-02 22:26

    you can use directly (blur) event in input tag.

    <div>
       <input [value] = "" (blur) = "result = $event.target.value" placeholder="Type Something">
       {{result}}
    </div>
    

    and you will get output in "result"

    0 讨论(0)
  • 2020-12-02 22:28

    This is the proposed answer on the Github repo:

    // example without validators
    const c = new FormControl('', { updateOn: 'blur' });
    
    // example with validators
    const c= new FormControl('', {
       validators: Validators.required,
       updateOn: 'blur'
    });
    

    Github : feat(forms): add updateOn blur option to FormControls

    0 讨论(0)
提交回复
热议问题