AngularJS ng-controller Directive

Definition and Usage

ng-controller Directives add controllers to your application.

In the controller, you can write code and create functions and variables, which will be part of the object and available in the current HTML element. In AngularJS, this object is called scope.

Instance

Add a controller to handle your application variables:

<div ng-app="myApp" ng-controller="myCtrl">
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "Bill";
    $scope.lastName = "Gates";
});
</script>

Try it yourself

Syntax

<element ng-controller="expression</element>

All HTML elements support it.

Parameters

Parameters Description
expression The name of the controller.