BenSV
BenSV

Reputation: 169

How to do routing in Angular correctly

I'm developing an web application using Angular latest version and SpringBoot for learning purposes.

I'm now facing a weird problem.I did the routing correctly and the http://localhost:4200 navigates to the home and when http://localhost:4200/bookings navigates to the booking component.But in that booking component I created a form and added css styles.The routing worked fine.But then I added

required[(ngModel)]="" 

part for every input field according to the tutorial.Now the problem is when I type http://localhost:4200/bookings It doesn't navigate to the booking component in-staid it returns the home component without loading the booking Component.If I remove the required[(ngModel)]="" part from every input field then the routing works fine and the http://localhost:4200/bookings will return the booking form.I'm currently stuck in that please help me to solve it.Why adding required[(ngModel)]="" part in every input field affect the routing?.

Navigation bar

nav role="navigation">
    <ul>
      <li><a routerLink="home">HOME</a></li>
      <li><a href="#">PLAN AND BOOK</a>
        <ul class="dropdown">
          <li><a routerLink="bookings" routerLinkActive="active">Manage My Booking</a></li>
          <li><a href="#">Check-In Online</a></li>
          <li><a href="#">Flight Status</a></li>
        </ul>
      </li>
      <li><a href="#">FLYINGWINGS</a></li>
      <li><a href="#">HOLIDAYS</a></li>
    </ul>
</nav>

Routing file

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'bookings', component: BookingComponent },
  { path: 'home', component: HomeComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

Booking.component.html file

<form (ngSubmit)="save()">     
<table style="width: 100%;">
    <tbody>
    <tr style="height: 74px;">
        <td style="height: 74px; width: 14%;">&nbsp;</td>
        <td style="height: 74px; width: 3%;">&nbsp;</td>
        <td style="height: 74px; width: 17%;">&nbsp;</td>
        <td style="height: 74px; width: 3%;">&nbsp;</td>
        <td style="height: 74px; width: 18%;">&nbsp;</td>
        <td style="height: 74px; width: 3%;">&nbsp;</td>
        <td style="height: 74px; width: 18%;">&nbsp;</td>
    </tr>
    <tr style="height: 39px;">
        <td style="height: 39px; width: 14%;">
            <div class="input-group">
            <div class="input-group-prepend">
            <div class="input-group-text">From</div>
            </div>
            <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Departure Airport" required[(ngModel)]="booking.departure">
            </div>
        </td>
    
        <td style="height: 39px; width: 3%;">&nbsp;</td>
    
        <td style="height: 39px; width: 17%;"> 
            <div class="input-group">
            <div class="input-group-prepend">
            <div class="input-group-text">To</div>
            </div>
            <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Arival Airport" required[(ngModel)]="booking.arrival" >
            </div>
        </td>
    
        <td style="height: 39px; width: 3%;">&nbsp;</td>
    
        <td style="height: 39px; width: 18%;"> 
            <div class="input-group">
            <div class="input-group-prepend">
            <div class="input-group-text">Depart</div>
            </div>
            <ejs-datetimepicker id='datetimepicker' placeholder='Select a date' [value]='dateValue' [min]='minDate' [max]='maxDate' required[(ngModel)]="booking.departd"></ejs-datetimepicker>
            </div>
        </td>
    
        <td style="height: 39px; width: 3%;">&nbsp;</td>
    
        <td style="height: 39px; width: 18%;">
            <div class="input-group">
            <div class="input-group-prepend">
            <div class="input-group-text">Return</div>
            </div>
            <ejs-datetimepicker id='datetimepicker' placeholder='Select a date' [value]='dateValue' [min]='minDate' [max]='maxDate' required[(ngModel)]="booking.returnd"></ejs-datetimepicker>
            </div>
        </td>
    </tr>
    
    <tr style="height: 39px;">
        <td style="height: 39px; width: 14%;">&nbsp;</td>
        <td style="height: 39px; width: 3%;">&nbsp;</td>
        <td style="height: 39px; width: 17%;">&nbsp;</td>
        <td style="height: 39px; width: 3%;">&nbsp;</td>
        <td style="height: 39px; width: 18%;">&nbsp;</td>
        <td style="height: 39px; width: 3%;">&nbsp;</td>
        <td style="height: 39px; width: 18%;">&nbsp;</td>
    </tr>
    
    
    <tr style="height: 39px;">
    <td style="height: 39px; width: 14%;"> 
        <div class="input-group">
        <div class="input-group-prepend">
          <div class="input-group-text">Class</div>
        </div>
       <select class="form-control" required[(ngModel)]="booking.class">
        <option>Economy</option>
        <option>Business</option>
       </select>
       </div>
    </td>
    
    <td style="height: 39px; width: 3%;">&nbsp;</td>
    
    <td style="height: 39px; width: 17%;">
        <div class="input-group">
        <div class="input-group-prepend">
          <div class="input-group-text">Adults</div>
        </div>
       <select class="form-control" required[(ngModel)]="booking.adults">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
        <option>8</option>
        <option>9</option>
      </select>
      </div>
    </td>
    
    <td style="height: 39px; width: 3%;">&nbsp;</td>
    
    <td style="height: 39px; width: 18%;">
        <div class="input-group">
        <div class="input-group-prepend">
          <div class="input-group-text">Children</div>
        </div>
       <select class="form-control" required[(ngModel)]="booking.children">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
        <option>8</option>
        <option>9</option>
      </select>
      </div>
    </td>
    
    <td style="height: 39px; width: 3%;">&nbsp;</td>
    
    <td style="height: 39px; width: 18%;">
        <div class="input-group">
        <div class="input-group-prepend">
          <div class="input-group-text">Infants</div>
        </div>
       <select class="form-control" required[(ngModel)]="booking.infants" >
           
        <option>0</option>
        <option>1</option>
       </select>
         </div>
    </td>
    </tr>
    
    <tr style="height: 32px;">
    <td style="height: 32px; width: 14%;"></td>
    <td style="height: 32px; width: 3%;">&nbsp;</td>
    <td style="height: 32px; width: 17%;">&nbsp;</td>
    <td style="height: 32px; width: 3%;">&nbsp;</td>
    <td style="height: 32px; width: 18%;">&nbsp;</td>
    <td style="height: 32px; width: 3%;">&nbsp;</td>
    <td style="height: 32px; width: 18%;">&nbsp;</td>
    </tr>
    <tr style="height: 32px;">
        <td style="height: 32px; width: 14%;"><button type="submit" class="btn btn-success">Search</button>
        </td>
        <td style="height: 32px; width: 3%;">&nbsp;</td>
        <td style="height: 32px; width: 17%;">&nbsp;</td>
        <td style="height: 32px; width: 3%;">&nbsp;</td>
        <td style="height: 32px; width: 18%;">&nbsp;</td>
        <td style="height: 32px; width: 3%;">&nbsp;</td>
        <td style="height: 32px; width: 18%;">&nbsp;</td>
        </tr>
    </tbody>
    </table>
</form>

Booking.Component.ts

export class BookingComponent implements OnInit {
  public minDate: Date = new Date ("01/01/2000 00:00 AM");
  public maxDate: Date = new Date ("01/01/2050 00:00 AM");
  public dateValue: Date = new Date ("01/01/2015 00:00 AM");

  booking:Bookings =new Bookings();
 


  constructor(private flightService:FlightService, private router: Router) { }

  

  ngOnInit(): void {
  }

  save() {
    this.flightService.search(this.booking)
      .subscribe(data => console.log(data), error => console.log(error));
    this.booking = new Bookings();
    this.gotoList();
  }

  gotoList() {
    this.router.navigate(['/home']);
  }

}

App.Module.ts file

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BookingComponent } from './booking/booking.component';
import { HeaderComponent } from './header/header.component';
import { CarouselModule } from 'ngx-bootstrap/carousel';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { HomeComponent } from './home/home.component';
import {HttpClientModule} from '@angular/common/http';
import { FooterComponent } from './footer/footer.component'
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars';


@NgModule({
  declarations: [
    AppComponent,
    BookingComponent,
    HeaderComponent,
    HomeComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    DateTimePickerModule,
    HttpClientModule,
    CarouselModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Please help Me.Thank you.

Upvotes: 0

Views: 39

Answers (1)

Usman Ali
Usman Ali

Reputation: 366

Example of two way binding

In .ts file

name: string;

In .html file

<input  [(ngModel)]="name" value="" required>
<p> {{name}} </p>

what you write in input it shows in

In your code, there is no problem with routing. The problem is with ngModel you have to clear in your problem that what you want to do by using [(ngModel)].

Upvotes: 1

Related Questions