0

My app.component.html code is :

    <h1>Password Generator</h1>
<div>
<label>Length</label>
</div>
<input (input)="onChangeLength($event.target.value)"/>
<div>
<div>
  <input (change)="onChangeHandlerLetter()" type="checkbox" />
  <label>Use Letter</label>
</div>
<div>
  <input (change)="onChangeHandlerNumbers()" type="checkbox" />
  <label>Use Numbers</label>
</div>
<div>
  <input (change)="onChangeHandlerSymbols()" type="checkbox" />
  <label>Use Symbols</label>
</div>
<div>
  <button (click)="onButtonClick()">Generate</button>
  </div>
</div>
<div>
  <label>Your Password</label>
</div>
<input [value]="password"/>

{{password}}

My app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  length=0;
  includeLetters=false;
  includeSymbols=false;
  includeNumbers=false
  onChangeHandlerLetter=()=>{
    this.includeLetters=!this.includeLetters
  }
  onChangeHandlerSymbols=()=>{
    this.includeSymbols=!this.includeSymbols
  }
  onChangeHandlerNumbers=()=>{
    this.includeNumbers=!this.includeNumbers
  }
  onChangeLength=(val:string)=>{
    const parsedValue=parseInt(val);
    if(!isNaN(parsedValue)){
      this.length=parsedValue
    }
    console.log(length);
  }
  password=" "
  onButtonClick=()=>{
  this.password="My Password"
  console.log("button clicked");
  console.log(`Following should be included :
              Sybmols : ${this.includeSymbols}
              Numbers : ${this.includeNumbers}
              Letters : ${this.includeLetters}`)
 }

}

error :

index.js:561 [webpack-dev-server] ERROR
src/app/app.component.html:5:46 - error TS2531: Object is possibly 'null'.

5 <input (input)="onChangeLength($event.target.value)"/>
                                               ~~~~~

  src/app/app.component.ts:5:16
    5   templateUrl: './app.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component AppComponent.

Hello greetings community , I am new to TS and angular so I dont know why I am getting this error I need to take input from input bar and change into number and console.log I am following a tutorial and he is getting correct output I dont know why

2
  • 1
    hi! that issue is because when input starts it has no value or when you delete all the characters, so the event lauch by the event can be null. You can use two thigs. try with this onChangeLength($event.target?.value) or try by only sending $event onChangeLength($event) and checking on the code inside onChangeLength if the event is receiving a value Commented Oct 18, 2022 at 6:12
  • did but getting 0 in console.log? what does that means Commented Oct 18, 2022 at 6:30

1 Answer 1

2

You need to tell TypeScript the type of the HTMLElement which is your target.

To do so instead of $event.target.value please use $event as below:

<input (input)="onChangeLength($event)"/>

then in app.component.ts use:

const parsedValue=parseInt((<HTMLInputElement>event.target).value);

Working codes:

app.component.ts

import { Component } from '@angular/core';
import { ColdObservable } from 'rxjs/internal/testing/ColdObservable';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  length=0;
  includeLetters=false;
  includeSymbols=false;
  includeNumbers=false
  onChangeHandlerLetter=()=>{
    this.includeLetters=!this.includeLetters
  }
  onChangeHandlerSymbols=()=>{
    this.includeSymbols=!this.includeSymbols
  }
  onChangeHandlerNumbers=()=>{
    this.includeNumbers=!this.includeNumbers
  }
  onChangeLength=( event: Event)=>{
    
     const parsedValue=parseInt((<HTMLInputElement>event.target).value);
     if(!isNaN(parsedValue)){
       this.length=parsedValue
     }
     console.log(length);
  }
  password=" "
  onButtonClick=()=>{
  this.password="My Password"
  console.log("button clicked");
  console.log(`Following should be included :
              Sybmols : ${this.includeSymbols}
              Numbers : ${this.includeNumbers}
              Letters : ${this.includeLetters}`)
 }

}

app.component.html

<h1>Password Generator</h1>
<div>
<label>Length</label>
</div>
<input (input)="onChangeLength($event)"/>
<div>
<div>
  <input (change)="onChangeHandlerLetter()" type="checkbox" />
  <label>Use Letter</label>
</div>
<div>
  <input (change)="onChangeHandlerNumbers()" type="checkbox" />
  <label>Use Numbers</label>
</div>
<div>
  <input (change)="onChangeHandlerSymbols()" type="checkbox" />
  <label>Use Symbols</label>
</div>
<div>
  <button (click)="onButtonClick()">Generate</button>
  </div>
</div>
<div>
  <label>Your Password</label>
</div>
<input [value]="password"/>

{{password}}
Sign up to request clarification or add additional context in comments.

1 Comment

Oshadharon bhai Angular o suru kore felsen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.