MEAN Angular Routing and Forms 1 — Questions and Answers
Question 1: Which Angular module must be imported to enable routing in an Angular application?
- HttpClientModule
- RouterModule (Correct answer)
- FormsModule
- BrowserAnimationsModule
Correct answer: RouterModule
RouterModule provides the Router service and directives like routerLink and router-outlet needed for navigation between views.
Question 2: How do you define a route with a URL parameter named 'id' in an Angular routing configuration?
- { path: 'item/{id}', component: ItemComponent }
- { path: 'item/:id', component: ItemComponent } (Correct answer)
- { path: 'item/[id]', component: ItemComponent }
- { path: 'item/$id', component: ItemComponent }
Correct answer: { path: 'item/:id', component: ItemComponent }
Angular uses the colon syntax (:id) to define route parameters that can later be accessed via the ActivatedRoute service.
Question 3: Which directive marks the location in an Angular template where the routed component should be rendered?
- <ng-template>
- <ng-content>
- <router-outlet> (Correct answer)
- <ng-view>
Correct answer: <router-outlet>
The <router-outlet> directive is the placeholder Angular uses to dynamically render the component associated with the currently active route.
Question 4: How do you programmatically navigate to the '/dashboard' route in an Angular component?
- this.location.go('/dashboard');
- window.location.href = '/dashboard';
- this.router.navigate(['/dashboard']); (Correct answer)
- this.route.navigateTo('/dashboard');
Correct answer: this.router.navigate(['/dashboard']);
The Router.navigate() method accepts an array of route path segments and navigates to the specified route programmatically.
Question 5: What service should you inject to access URL route parameters inside an Angular component?
- Router
- RouterLink
- RouteConfig
- ActivatedRoute (Correct answer)
Correct answer: ActivatedRoute
ActivatedRoute provides observables for the current route's parameters (params), query parameters (queryParams), and static route data.
Question 6: What is the purpose of the 'canActivate' route guard interface in Angular?
- To resolve data before a route is activated
- To prevent navigation away from a route
- To control whether a user can navigate to a route (Correct answer)
- To lazy-load a module when its route is first accessed
Correct answer: To control whether a user can navigate to a route
canActivate is a guard interface that runs a check before a route is activated, returning true to allow navigation or false to block it.
Question 7: What is lazy loading in the context of Angular routing?
- Delaying route parameter resolution until the component is fully rendered
- Loading feature modules only when their associated routes are first accessed (Correct answer)
- Preloading all application modules at startup in the background
- Loading route guards asynchronously after the component mounts
Correct answer: Loading feature modules only when their associated routes are first accessed
Lazy loading defers loading of Angular feature modules until the user first navigates to their routes, reducing the initial bundle size.
Which Angular module must be imported to enable routing in an Angular application?