In: Computer Science
4 visa
5 mastercard
34 and 37 amex
30, 36, 38, 39 diners
60, 64, 65 discover
import { Component, OnInit } from 'angular/core';
@Component({
selector: 'app-root',
templateUrl: '.app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements onInit {
title = 'title';
x: number;
ngOnInit(){
this.x = Math.floor(Math.random()*10);
}
}
Here is the app.component.html file:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular</title>
<base href="/">
<meta name="viewport" content="width=device-width,
initial-scale=1">
</head>
<body>
<app-root></app-root>
<div style="text-align:center">
<h1>
Welcome to {{x}}.
</h1>
</div>
<div [ngswitch]="switch_expression">
<p *ngSwitchCase="1">A</p>
<p *ngSwitchCase="2">B</p>
<p *ngSwitchCase="3">C</p>
<p *ngSwitchCase="4">D</p>
<p *ngSwitchDefault>F</p>
</div>
</body>
</html>
f.As C does not have an inbuilt function for generating a number in the range, but it does have rand function which generate a random number from 0 to RAND_MAX. With the help of rand () a number in range can be generated as num = (rand() % (upper – lower + 1)) + lower
// C program for generating a
// random number in a given range.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count)
{
int i;
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
// Driver code
int main()
{
int lower = 5, upper = 7, count = 1;
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
return 0;
}
Output:
7
11
Three things to keep in mind ngSwitch, ngSwitchCase and ngSwitchDefault.
ngSwitch - set the property value of model. For example - viewMode, which is a property in your component.
ngSwitchCase - Defines what to render when the value of the property defined in ngSwitchChanges. For ex. when viewMode = 'map'
ngSwitchDefault - Defines what to render if the value doesn't match. For ex. when viewMode=undefined. The default will be rendered.
Another important point is that we need to set the [ngSwitchCase] within a <template></template> HTML element otherwise it will not work. Angularwill automatically convert it into a <div> tag.
<div [ngSwitch]="'viewMode'">
<template [ngSwitchCase]="'map'" ngSwitchDefault>
Map View Content...
</template>
<template [ngSwitchCase]="'list'">
List View Content...
</template>
</div>