Slip 1(a) Write an Angular 13 application addition of two numbers using ng-init, ng-model & ng-bind. And also demonstrate ng-show, ng-disabled, ng-click directives on button component.

 a) ng new slip1

b) cd slip1

c) open app.component.ts

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


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  num1: number = 0;
  num2: number = 0;
  result: number = 0;
  disableButton: boolean = false;
  showResult: boolean = false;

  add(): void {
    this.result = this.num1 + this.num2;
    this.disableButton = true;  // Disable the button after calculation
    this.showResult = true;      // Show the computed result
  }
}                                                                                        

2)app.component.html
<div>
    <h2>Add Two Numbers</h2>

    <label>
      First Number:
      <input type="number" [(ngModel)]="num1">
    </label>
    <br>

    <label>
      Second Number:
      <input type="number" [(ngModel)]="num2">
    </label>
    <br><br>

    <button (click)="add()" [disabled]="disableButton">
      Compute Sum
    </button>
    <br><br>

    <h3 *ngIf="showResult">
      Result: {{ result }}
    </h3>
</div>

3) app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }



Run project -
ng serve -o









Comments

Popular posts from this blog

Slip 11: Write node js application that transfer a file as an attachment on web and enables browser to prompt the user to download file using express js.

Slip 7 Create a node js file named main.js for event-driven application. There should be a main loop that listens for events, and then triggers a callback function when one of those events is detected.