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]
Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…
In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…
In this blog, I aim to provide a comprehensive list of valuable resources for learning…
Have you ever wondered how systems determine whether to grant or deny access, and how…
What revolutionary technologies and industries will define the future of business in 2025? As we…
For data scientists and machine learning researchers, 2024 has been a landmark year in AI…