0

I have the following code in stock-status.component.ts file

@Component({
  selector: 'app-stock-status',
  template:`
  <input type="number" value='0' min='0' required [(ngModel)]='updatedStockValue'/>
  <button class="btn btn-primary" [style.background]='color' (click)='stockValueChanged()'>Change Stock Value</button>
    
  `,
  styleUrls: ['./stock-status.component.css']
})

As you can see in the image it is not showing the default value as 0 enter image description here

And whenever I click the Button when no data is initialized , it shows NaN which is not what I want

Any Kind of Help and Guidance would be appreciated

2 Answers 2

1

The ngModel binding might have precedence here. You could ignore the value attribute and set updatedStockValue in it's definition.

Try the following

@Component({
  selector: 'app-stock-status',
  template:`
  <input type="number" min="0" required [(ngModel)]="updatedStockValue"/>
  <button class="btn btn-primary" [style.background]="color" (click)="stockValueChanged()">Change Stock Value</button>
    
  `,
  styleUrls: ['./stock-status.component.css']
})
export class AppComponent {
  updatedStockValue: number = 0;
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can initialize a variable in the template with ng-init if you don't want to do it in the controller.

  <input type="number" min='0' required [(ngModel)]='updatedStockValue'
  ng-init="updatedStockValue=0"/>

1 Comment

Thanks for the answer but it doesn't seem to work and has the same bugs as that of my code @Michael's answer works great

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.