MOCKSTACKS
EN
Questions And Answers

More Tutorials







Angular Tutorial

Getting Started

Create a new HTML file and paste the following content:

<!DOCTYPE html>
<html ng-app>
<head>
 <title>Hello, Angular</title>
 <script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
</head>
<body ng-init="name='World'">
 <label>Name</label>
 <input ng-model="name" />
 <span>Hello, {{ name }}!</span>
 <p ng-bind="name"></p>
</body>
</html>

When you open the file with a browser, you will see an input field followed by the text Hello, World!. Editing the value in the input will update the text in real-time, without the need to refresh the whole page.

Explanation:



1. Load the Angular framework from a Content Delivery Network.

<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>

2. Define the HTML document as an Angular application with the ng-app directive

<html ng-app>

3. Initialize the name variable using ng-init

<body ng-init=" name = 'World' ">


Note that ng-init should be used for demonstrative and testing purposes only. When building an actual application, controllers should initialize the data.

4. Bind data from the model to the view on HTML controls. Bind an < input > to the name property with ng-model

<input ng-model="name" />

5. Display content from the model using double braces {{ }}

<span>Hello, {{ name }}</span>

6. Another way of binding the name property is using ng-bind instead of handlebars"{{ }}"

<span ng-bind="name"></span>

The last three steps establish the two way data-binding. Changes made to the input update the model, which is reflected in the view.
There is a difference between using handlebars and ng-bind. If you use handlebars, you might see the actual Hello, {{name}} as the page loads before the expression is resolved (before the data is loaded) whereas if you use ng-bind, it will only show the data when the name is resolved. As an alternative the directive ng-cloak can be used to prevent handlebars to display before it is compiled.


Conclusion

In this page (written and validated by ) you learned about AngularJS Tutorial . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS constructs.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.