The $scope
variable is the equivalent of a .NET DataContext
which is used to establish the root of any data binding expression which used in the corresponding HTML markup:
function UserController($scope) {
$scope.currentUser = {
firstName: "John",
lastName: "Doe"
};
}
function DocumentController($scope) {
$scope.document = {
title: "My Document",
pageCount: 123
};
}
To associate a certain controller with an HTML fragment, you have to add the ng-controller attribute to it to reference the desired controller.
<div ng-app ng-controller="UserController">
First Name: <input type=text ng-model="currentUser.firstName"> <br>
Last Name: <input type=text ng-model="currentUser.lastName"><br>
Full Name: {{currentUser.firstName}} {{currentUser.lastName}}
</div>
<div ng-app ng-controller="DocumentController">
Title: <input type=text ng-model="document.Title"> <br>
Page Count: <input type=text ng-model="document.pageCount"><br>
</div>