0 votes
in Angular by
How are Angular expressions different from JavaScript expressions?

1 Answer

0 votes
by
The first and perhaps, the biggest difference is that Angular expressions allow us to write JavaScript in HTML which is not the case when it comes to JavaScript expressions.

Next, Angular expressions are evaluated against a local scope object whereas JavaScript expressions against global window object. Let's understand that better with an example :

Consider the following component named test:

    

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

      @Component({

        selector: 'app-test',

        template: `

            <h4>{{message}}</h4>

        `,

        styleUrls: ['./test.component.css']

      })

      export class TestComponent implements OnInit {

        message:string = “Hello world”;

        constructor() { }

        ngOnInit() {

        }

      }

    

  

As one can see that Angular expression is used to display message property of a component. Since we are using Angular expressions, in the present template, we cannot access a property outside of its local scope, which in this case is TestComponent.

This proves that Angular expressions are always evaluated based on scope object rather than the global object.

Next difference is how Angular expressions handle null and undefined.

Consider the following JavaScript example:

    

      <!DOCTYPE html>

      <html lang="en">

      <head>

          <meta charset="UTF-8">

          <meta name="viewport" content="width=device-width, initial-scale=1.0">

          <title>JavaScript Test</title>

      </head>

      <body>

          <div id="foo"><div>

      </body>

      <script>

          'use strict';

          let bar = {};

          document.getElementById('foo').innerHTML = bar.x;

      </script>

      </html>

    

  

If you run the above code, you will 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 consider the following Angular example:

    

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

      @Component({

        selector: 'app-new',

        template: `

            <h4>{{message}}</h4>

        `,

        styleUrls: ['./new.component.css']

      })

      export class NewComponent implements OnInit {

        message:object = {};

        constructor() { }

        ngOnInit() {

        }

      }

    

  

If you render the above component, you will not see undefined being displayed on the screen.

Next, in Angular expressions one cannot use loops, conditionals and exceptions.

The difference which makes Angular expressions quite beneficial is the use of pipes. Angular uses pipes(called filters in AngularJS), which can be used to format data before displaying it. Let’s see one predefined pipe in action:

    

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

      @Component({

        selector: 'app-new',

        template: `

            <h4>{{message | lowercase}}</h4>

        `,

        styleUrls: ['./new.component.css']

      })

      export class NewComponent implements OnInit {

        message:string = "HELLO WORLD";

        constructor() { }

        ngOnInit() {

        }

      }

    

  

In the above code we have used a predefined pipe called lowercase, which transforms all the letters in lowercase. Therefore, if you render the above component, you will see “hello world” being displayed.

In contrast, JavaScript does not have the concept of pipes.

Related questions

0 votes
asked Dec 29, 2023 in Angular by DavidAnderson
0 votes
asked Jun 5, 2022 in Angular by Robindeniel
...