1. Home
  2. Languages
  3. AngularJS
  4. How to Use ng-init in AngularJS to Set Default Values Effectively

How to Use ng-init in AngularJS to Set Default Values Effectively


2. Use ng-init to Set Default Values in Your HTML

Inside the body, where you want to initialize your variable:

Here, username is set to 'Guest' and age is set to 25 by default.

3. Display the Variables in Your Template

You can display these initialized variables anywhere within the scope using AngularJS bindings:

Welcome, {{ username }}!

Your age is {{ age }}.

Updated on July 5, 2025
Was this article helpful?

Related Articles

Leave a Comment