Special welcome gift. Get 50% off your first courses with code “AS50”. Find out more!

HomeUncategorizedThe most crucial difference between Angular expressions and JavaScript expressions is that the Angular expressions allow us to write JavaScript in HTML. On the other hand, the JavaScript expressions don’t allow.

The most crucial difference between Angular expressions and JavaScript expressions is that the Angular expressions allow us to write JavaScript in HTML. On the other hand, the JavaScript expressions don’t allow.

2. The Angular expressions are evaluated against a local scope object. On the other hand, the JavaScript expressions are evaluated against the global window object. We can understand it better with an example. Suppose we have a component named test:

  1. import { Component, OnInit } from ‘@angular/core’;  
  2. @Component({  
  3. selector: ‘app-test’,  
  4. template: `  
  5. <h4>{{message}}</h4>,  
  6. styleUrls: [‘./test.component.css’]  
  7. })  
  8. export class TestComponent implements OnInit {  
  9. message:string = ?Hello world?;  
  10. constructor() { }  
  11. ngOnInit() {  
  12. }  
  13. }  

In the above example, we can see that the Angular expression is used to display the message property. In the present template, we are using Angular expressions, so we cannot access a property outside its local scope (in this case, TestComponent). This proves that Angular expressions are always evaluated based on the scope object rather than the global object.

3. The Angular expressions can handle null and undefined, whereas JavaScript expressions cannot.

See the following JavaScript example:

  1. <!DOCTYPE html>  
  2. <html lang=”en”>  
  3. <head>  
  4.     <meta charset=”UTF-8″>  
  5.     <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>  
  6.     <title>JavaScript Test</title>  
  7. </head>  
  8. <body>  
  9.     <div id=”foo”><div>  
  10. </body>  
  11. <script>  
  12.     ‘use strict’;  
  13.     let bar = {};  
  14.     document.getElementById(‘foo’).innerHTML = bar.x;  
  15. </script>  
  16. </html>  

After running the above code, you see undefined displayed on the screen. Although it’s not ideal to leave any property undefined, the user does not need to see this.

Now see the following Angular example:

  1. import { Component, OnInit } from ‘@angular/core’;  
  2. @Component({  
  3.   selector: ‘app-new’,  
  4.   template: `  
  5.       <h4>{{message}}</h4>       `,  
  6.   styleUrls: [‘./new.component.css’]  
  7. })  
  8. export class NewComponent implements OnInit {  
  9.   message:object = {};  
  10.   constructor() { }  
  11.   ngOnInit() {  
  12.   }  
  13. }  

In the above example, you will not see undefined being displayed on the screen.

4. In Angular expressions, we cannot use loops, conditionals, and exceptions. The difference which makes Angular expressions quite beneficial is the use of pipes. Angular uses pipes (known as filters in AngularJS) to format data before displaying it.

See this example:

  1. import { Component, OnInit } from ‘@angular/core’;  
  2.       @Component({  
  3.         selector: ‘app-new’,  
  4.         template: `  
  5.             <h4>{{message | lowercase}}</h4>,  
  6.         styleUrls: [‘./new.component.css’]  
  7.       })  
  8.       export class NewComponent implements OnInit {  
  9.         message:string = “HELLO JAVATPOINT”;  
  10.         constructor() { }  
  11.         ngOnInit() {  
  12.         }  
  13.       }   

In the above example, we have used a predefined pipe called lowercase, which transforms all the letters in lowercase. If you run the above example, you will see the output displayed as “hello javatpoint”.

On the other hand, JavaScript does not have the concept of pipes.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

Categories

ads sidebar 1

You May Also Like

An event-driven programming approach uses events to trigger various functions. An event can be anything, such as typing a key...
In angularJS, a module is a process to group directives, and services components that are related. It arranges them in...
AngularJS is one of the most popular, open-source, JavaScript-based frameworks, developed by Google, that was mainly built for developing large-scale,...