Below is an example to combine the first name and last name using AngularJS Controller.
index.html:
SPController
function is a regular JavaScript function.
[html]
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.3.14" data-semver="1.3.14" src="https://code.angularjs.org/1.3.14/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script src="App.js"></script>
<script src="Controller.js"></script>
</head>
<body>
<h1>AngularJS Controllers</h1>
<div ng-app="spApp" ng-controller="SPController">
First Name:
<input type="text" ng-model="firstName">
<br> <br>
Last Name:
<input type="text" ng-model="lastName">
<br><br>
Full Name: {{firstName + " " + lastName}}
</div>
</body>
</html>
[/html]
Controller.js
- AngularJS can be called with a controller
$scope
object.
$scope
is the owner of application data within the SPController
.
$scope.firstname
and $scope.lastname
are the properties created by the SPController
.
[javascript]
myApp.controller('SPController', function($scope) {
$scope.firstName = "Michael",
$scope.lastName = "Jackson"
});
[/javascript]
App.js
[javascript]
var myApp = angular.module("spApp", []);
[/javascript]
Output:
In the above demo,
SPController is defined as a function.
$scope
is an object that is passed as an argument for controller and this object can be used to bind the data from application controller to the view. More detailed information about scope object is available in the chapter
AngularJS - Scope topic.
Note:
$scope
is passed as a parameter by each controller, which alludes to the application data within that specific controller.