Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Angular Interview Questions and Answers

Related differences

Related differences

Angular vs ReactAngularJS vs NodeJSAngularJS vs Angular
Jquery vs AngularJS

Ques 21. What is the purpose of async pipe?

The AsyncPipe subscribes to an observable or promise and returns the latest value it has emitted. When a new value is emitted, the pipe marks the component to be checked for changes.

Let's take a time observable which continuously updates the view for every 2 seconds with the current time.

@Component({

  selector: 'async-observable-pipe',

  template: `<div><code>observable|async</code>: Time: {{ time | async }}</div>`

})

export class AsyncObservablePipeComponent {

  time = new Observable(observer => setInterval(() => observer.next(new Date().toString()), 2000) );

}

Is it helpful? Add Comment View Comments
 

Ques 22. What is the option to choose between inline and external template file?

You can store your component's template in one of two places. You can define it inline using the template property, or you can define the template in a separate HTML file and link to it in the component metadata using the @Component decorator's templateUrl property.

The choice between inline and separate HTML is a matter of taste, circumstances, and organization policy. But normally we use inline template for small portion of code and external template file for bigger views. By default, the Angular CLI generates components with a template file. But you can override that with the below command,

ng generate component hero -it

Is it helpful? Add Comment View Comments
 

Ques 23. What is the purpose of ngFor directive?

We use Angular ngFor directive in the template to display each item in the list. For example, here we iterate over list of users,

<li *ngFor="let user of users">

  {{ user }}

</li>

The user variable in the ngFor double-quoted instruction is a template input variable

Is it helpful? Add Comment View Comments
 

Ques 24. What is the purpose of ngIf directive in Angular?

Sometimes an app needs to display a view or a portion of a view only under specific circumstances. The Angular ngIf directive inserts or removes an element based on a truthy/falsy condition. Let's take an example to display a message if the user age is more than 18,

<p *ngIf="user.age > 18">You are not eligible for student pass!</p>

Note: Angular isn't showing and hiding the message. It is adding and removing the paragraph element from the DOM. That improves performance, especially in the larger projects with many data bindings.

Is it helpful? Add Comment View Comments
 

Ques 25. What happens if you use script tag inside template?

Angular recognizes the value as unsafe and automatically sanitizes it, which removes the script tag but keeps safe content such as the text content of the script tag. This way it eliminates the risk of script injection attacks. If you still use it then it will be ignored and a warning appears in the browser console.

Let's take an example of innerHtml property binding which causes XSS vulnerability,

export class InnerHtmlBindingComponent {

  // For example, a user/attacker-controlled value from a URL.

  htmlSnippet = 'Template <script>alert("0wned")</script> <b>Syntax</b>';

}

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook