Skip to main content

angular 2 template driven forms

From:

A form contains various input components like text, option, select option and pickers. This input component behavior is manipulated into Form.  A form is operated to two-way data binding, validation, event binding and error handling while submitting the form or inline validate.

Template Driven form:

A Template Driven Form creates a set angular structure based directives.


Example:

I create execute the following angular-CLI code: 

ng g component tempdrivenfrm

Now, the template driven component is created successfully.  We write a logic for the corresponding files.

HTML:

<form #usrfrm="ngForm" (ngSubmit)="userFormSubmit(usrfrm)">
<div class="form-group">
<div class="form-group">
    <label>User Name</label>
    <input type="text" #myNameRef="ngModel" required minlength="5" class="form-control" name="name" ngModel>    
    <div *ngIf=" myNameRef.errors && (myNameRef.dirty || myNameRef.touched)">
    <div [hidden]=" !myNameRef.errors.required ">
    Please enter the name
    </div>
    <div [hidden]=" !myNameRef.errors.minlength ">
    Please enter minum 5 character
    </div>
    </div>
  </div>
  <div class="form-group">
    <label>User email</label>
    <input type="text" #myEmailRef="ngModel" required class="form-control" name="email" ngModel >
  </div>  
  <div [hidden]=" myEmailRef.valid || myEmailRef.pristine " class="alert alert-danger">
  Please enter the email
  </div>  
  <div class="form-group">
    <label>User phone</label>
    <input type="phone" class="form-control" name="phone" ngModel>
  </div>
  <div ngModelGroup="address">
  <div class="form-group">
    <label>User street</label>
    <input type="text" class="form-control" name="street" ngModel>
  </div>
  <div class="form-group">
    <label>User city</label>
    <input type="text" class="form-control" name="city" ngModel>
  </div>
  </div>
  <button type="submit" class="btn btn-primary btn-lg"> Submit </button>
</div>
</form>


CSS:

input.ng-invalid {
border-left: 5px solid red;
}

input.ng-valid {
border-left: 5px solid green;
}

.error-message {
color: red;
padding-left: 10px; 
}

Typescript:

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

@Component({
  selector: 'app-tempdrivenfrm',
  templateUrl: './tempdrivenfrm.component.html',
  styleUrls: ['./tempdrivenfrm.component.css']
})
export class TempdrivenfrmComponent implements OnInit {
myName: string;
  constructor() { }

  ngOnInit() {
  this.myName = "";
  }
  userFormSubmit(frmValidation: any){
  console.log(frmValidation.value);
  }
}

Result:




Comments

Popular posts from this blog

How has lockdown impacted Indian farmers?

How has lockdown impacted Indian farmers?             Generally compare to other sectors it's not major impacted by our Indian farmers but we acceptable minor impacts. Here we separate major two farming cultures one is mono farming culture another one poly farming culture. Mono farming :             The major impacts from our Indian farmer for monoculture. Here I have briefly explained what are the things we had faced our local farmers. Here I explain two different crops type: Daily crops : * We harvest more than 100-kilo grams daily crops like brinjal but we unable to sell those in local markets. * We are unable to reach urban markets. * We don't have enough transport facilities. * We unable buy require fertilizer and other complements * In the early stage we are facing manpower shortage but later it's not an issue. * We unable to do value-added products. * Waste is very high for somedays Se...

How is a startup possible in farming and agriculture?

                 In this question we have two options, The first one is natural farming and another one is chemical farming. Both are the way to start an agriculture startup. Before we jump into the topic we have to look at more important things like Financial, Yield, quality of food and Marketing these are the very challenge we face. Chemical Farming 1. Capital & financial   * an agriculture startup we must have to invest for own land but chemical farming not require to buy an own land because chemical farming land does not produce yield more than 10 years so best you can acquire rental land.  * We have to buy cultivate equipment and machinery for chemical farming. In this case, we have two options       1. More necessity things are bought own equipment         2. Fewer necessities things go for a rental.   * Make a sufficient fund for runnin...

How will agriculture evolve

Agriculture will evolve the following types in the future. 1. Natural Farming 2. Nutrition Orient Farming 3. Fertilize Orient Farming 4. Terrace garden Farming 1. Natural Farming Now, most of people aware and important of natural food. We are consuming a lot of foods that were packed, preservatives and Jung, these items came from various places in the world so it's added lot of chemicals to avoid food waste. These things we could consume regularly till now. almost we are facing so many diseases in our day to life. So people are avoiding unhealthy food to use that is the reason we are switchover natural farming. The natural farming foods are very expensive nowadays but in the future may be it will reduce the prices. When this will happen If more people involve natural farming. 2. Nutrition Orient Farming It's like the way of doing the Hydroponic farming process system but the thing is only nutrition leaf harvesting. In this leaf contain all the nutrition, If y...