2. Structural Directives - ngIf
2.a Structural Directives - ngIf
Create a login form with username and password fields. If the user enters the correct credentials, it should render a "Welcome <<username>>" message otherwise it should render "Invalid Login!!! Please try again..." message
Step 1: Generate a new component
ng generate component login
login.component.html file and replace its content with the following code:Step 3: Update the login component class
Open the login.component.ts file and update its content with the following code:
import { Component } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
username: string="";
password: string="";
isLoggedIn: boolean = false;
isValidLogin: boolean = true;
submitForm() {
// Replace the following logic with your own authentication logic
if (this.username === 'yourUsername' && this.password === 'yourPassword') {
this.isLoggedIn = true;
this.isValidLogin = true;
} else {
this.isLoggedIn = false;
this.isValidLogin = false;
}
}
}
app.module.ts) where you want to use the LoginComponent and import it:declarations array in the @NgModule decorator:app.component.ts), define an array of courses:app.component.html), use the ngFor directive to iterate over the courses array and render it in a list format:app.component.ts), define a variable to hold the value that determines the option to display. Let's call it selectedOption:app.component.html), use the ngSwitch directive to determine which option to display based on the value of selectedOption:[ngSwitch] attribute to bind the selectedOption value to the ngSwitch directive. The ngSwitchCase directive is used to define the different options based on their values. The ngSwitchDefault directive is used to handle the case when none of the specified options match the selectedOption value.2.d. Custom Structural Directive
Create a custom structural directive called 'repeat'
which should repeat
the element
given a number
of times.
@Directive decorator. Structural directives in Angular manipulate the DOM by adding, removing, or updating elements based on the conditions they define.Create a new TypeScript file for the directive. For example,
repeat.directive.ts.Import the necessary dependencies:
@Directive decorator, specifying the selector and any inputs it might take:- Explanation of the code:
- We inject
TemplateRefandViewContainerRefinto the directive's constructor.TemplateRefrepresents the template associated with the directive, andViewContainerRefallows us to manipulate the view where the directive is applied. - We define an input property named
repeatusing the@Inputdecorator. This input will take the number of times the element should be repeated. - When the
repeatinput changes, theset repeatmethod will be called. Inside this method, we clear the view container and then create the embedded view of the templatetimesnumber of times. This will repeat the element accordingly.
- Finally, add the
RepeatDirectiveto your Angular module'sdeclarationsarray to make it available throughout the application.
For example, if you are using the directive in the AppModule, it will look like this
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { RepeatDirective } from './repeat.directive'; @NgModule({ declarations: [ AppComponent, RepeatDirective ], imports: [ BrowserModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Now, you can use the 'appRepeat' directive in your HTML templates like this:
Comments
Post a Comment