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

Contents


    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?

    Leave a Reply

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