The article presents hello world code sample for AngularJS javascript framework.
Following are important aspects to note while you are going through the Hello World demo and the code samples listed below.
Include following code within <head></head> to include Angularjs javascript file. Get the latest code such as below from Google hosted libraries page.
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
Apply ng-app directive to <html> element such as following. Giving name of the app is optional. It could be written as simple as <html ng-app>. This directive is used to flag the html element that Angular should consider to be the root element of our application. This gives application developers the freedom to tell Angular if the entire html page or only a portion of it should be treated as the Angular application.
<html ng-app="helloApp">
Apply ng-controller in body element. The controller directive can, however, be applied to any element such as div. In the code below, “HelloCtrl” is the name of the controller and is referred in the controller script written within <script></script> within <head> element.
<body ng-controller="HelloCtrl">
Bind the input with the model using ng-model directive.
<input type="text" name="name" ng-model="name"/>
Following is the template code displaying the model value for model with name as “name”. Pay attention that model with name as “name” is bounded with input in step 4.
Hello {{name}}! How are you doing today?
Create the controller code such as following within script element. In the code below, “helloApp” is the name of the module which is defined with ng-app directive within <html> element. The next line shows the code to create a controller with name as “HelloCtrl” which is the name defined with ng-controller directive within <body> element. The controller “HelloCtrl” is registered with the module, “helloApp”. The last line displays the model associated with the $scope object.
<script> var helloApp = angular.module("helloApp", []); helloApp.controller("HelloCtrl", function($scope) { $scope.name = "Calvin Hobbes"; }); </script> The full source code (for copy & paste & run!) could be found on this page.
[adsenseyu1]
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…