TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Angular 7 Property Binding

Angular 7 Property Binding with angular7, tutorial, introduction, angular, js, Installation, History and Versions, First App, files explanation, Components, Directives, ngIf Directive, ngFor Directive, ngClassDirective, ngStyle Directive, Angular7 with Bootstrap etc.

<< Back to ANGULAR

Angular 7 Property Binding

In Angular 7, property binding is used to pass data from the component class (component.ts) and setting the value of the given element in the user-end (component.html).

Property binding is an example of one-way databinding where the data is transferred from the component to the class.

The main advantage of property binding is that it facilitates you to control elements property.

For example

We are going to add a button to "component.html" page.

<p>
  Server2 is also working fine.
</p>
<button class="btn btn-primary">Add Server</button>

component.ts file:

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

@Component({
  selector: 'app-server2',
  templateUrl: './server2.component.html',
  styleUrls: ['./server2.component.css']
})
export class Server2Component implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

Output:

Angular 7 Property Binding

Let's see how property binding works?

First, we are going to disable the button by using disabled attribute.

<p>
  Server2 is also working fine.
</p>
<button class="btn btn-primary" disabled>Add Server</button>

Now, the button is disabled.

Let's add a new property "allowNewServer" in "component.ts" file which will disable the button automatically but after a specific (settable) time.

component.ts file:

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

@Component({
  selector: 'app-server2',
  templateUrl: './server2.component.html',
  styleUrls: ['./server2.component.css']
})
export class Server2Component implements OnInit {
 allowNewServer = false;

  constructor() {
    setTimeout(() =>{
      this.allowNewServer = true;
    }, 5000);
  }

  ngOnInit() {
  }

}

component.html file:

<p>
  Server2 is also working fine.
</p>
<button class="btn btn-primary"
        [disabled]="allowNewServer">Add Server</button>

Here, we set a time of 5000 millisecond or 5 second. After 5 seconds, the button will be disabled automatically.

This is an example of property binding where a property is bound dynamically.

Output:

Angular 7 Property Binding
<p>
  Server2 is also working fine.
</p>
<button class="btn btn-primary"
        [disabled]="!allowNewServer" >Add Server</button>

By using the above code, you can allow the disabled button after 5 seconds automatically.

Property Binding vs. String Interpolation

We can use property binding as well as string interpolation for databinding cases. For example, let's add string interpolation in the above example.

<p>
  Server2 is also working fine.
</p>
<button class="btn btn-primary"
        [disabled]="!allowNewServer" >Add Server</button>
<h3>{{allowNewServer}}</h3>

Here, <h3>{{allowNewServer}}</h3> specifies string interpolation.

Output:

Angular 7 Property Binding

We can do the same task by using property binding also.

Example:

<p>
  Server2 is also working fine.
</p>
<button class="btn btn-primary"
        [disabled]="!allowNewServer" >Add Server</button>
<h3 [innerText]= "allowNewServer"></h3>

Output:

It will also give you the same result:

Angular 7 Property Binding

But, string interpolation has some limitation. Later, we shall learn where to use string interpolation and where property binding.


Next TopicAngular 7 Pipes




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf