AngularJS
AngularJS Events: ng-click, ng-show, ng-hide [Example]
When creating web-based applications, sooner or later your application will need to handle DOM...
A Controllers in AngularJs takes the data from the View, processes the data, and then sends that data across to the view which is displayed to the end user. The Controller will have your core business logic.
The controller will use the data model, carry out the required processing and then pass the output to the view which in turn is displayed to the end user.
In this tutorial, you will learn-
Following is a simple definition of working of Angular JS Controller.
Before we start with the creation of a controller, we need to first have our basic HTML page setup in place.
The below code snippet is a simple HTML page which has a title of "Event Registration" and has references to important libraries such as Bootstrap, jquery and Angular.
By default the above code snippet will be present in all of our examples, so that we can show just the specific angularJS code in the subsequent sections.
Secondly let's look at our files and file structure which we are going to start with for the duration of our course.
Let see an example on how to use angular.js,
What we want to do here is just to display the words "AngularJS" in both text format and in a text box when the page is viewed in the browser.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<h1> gtupapers Global Event</h1>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="lib/angular.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="lib/jquery-1.11.3.min.js"></script>
<div ng-app="DemoApp" ng-controller="DemoController">
Tutorial Name : <input type="text" ng-model="tutorialName"><br>
This tutorial is {{tutorialName}}
</div>
<script>
var app = angular.module('DemoApp',[]);
app.controller('DemoController', function($scope){
$scope.tutorialName = "Angular JS";
});
</script>
</body>
</html>
Code Explanation:
If the command is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
Since we assigned the variable tutorialName a value of "Angular JS", this gets displayed in the text box and in the plain text line.
Normally, one would want to define multiple methods in the controller to separate the business logic.
For example, suppose if you wanted to have your controller do 2 basic things,
You would then ideally create 2 methods inside of your controller, one to perform the addition and the other to perform the subtraction.
Let's see a simple example of how you can define custom methods within an Angular.JS controller. The controller will simply return a string.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body ng-app="DemoApp">
<h1> gtupapers Global Event</h1>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="lib/angular.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="lib/jquery-1.11.3.min.js"></script>
<div ng-app="DemoApp" ng-controller="DemoController">
Tutorial Name :<input type="text" ng-model="tutorialName"><br>
<br>
This tutorial is {{tutorialName}}
</div>
<script>
var app = angular.module('DemoApp', []);
app.controller('DemoController', function($scope) {
$scope.tutorialName = "Angular JS";
$scope.tName = function() {
return $scope.tName;
};
});
</script>
</body>
</html>
Code Explanation:
Output:
Let's look at an example of "HelloWorld" where all of the functionality was placed in a single file. Now it's time to place the code for the controller in separate files.
Let's follow the steps below to do this.
Step 1) In the app.js file, add the following code for your controller
angular.module('app',[]).controller('HelloWorldCtrl',function($scope)
{
$scope.message = "Hello World"
});
The above code does the following things,
Step 2) Now, in your Sample.html file add a div class which will contain the ng-controller directive and then add a reference to the member variable "message"
Also don't forget to add a reference to the script file app.js which has the source code for your controller.
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<h1> gtupapers Global Event</h1>
<div class="container">
<div ng-controller="HelloWorldCtrl">{{message}}</div>
</div>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="lib/angular.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="lib/jquery-1.11.3.min.js"></script>
<script src="app.js"></script>
</body>
</html>
If the above code is entered correctly, the following Output will be shown when you run your code in the browser.
Output:
Summary
When creating web-based applications, sooner or later your application will need to handle DOM...
The best way to see the power of an AngularJS Application is to create your first basic program...
What is Dependency Injection in AngularJS? Dependency Injection is a software design pattern that...
What is an AngularJS directive? A directive in AngularJS is a command that gives HTML new...
Tables are one of the common elements used in HTML when working with web pages. Tables in HTML are...
What is Angular JS? AngularJS is an open-source front-end web framework based on JavaScript to build...