Thứ Tư, 11 tháng 3, 2015

AngularJS Controllers: ng-controller

AngularJS Controllers


---o0o---


Original link

1. AngularJS Controllers


<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
	First Name: <input type="text" ng-model="firstName"><br>
	Last Name: <input type="text" ng-model="lastName"><br>
	Full Name: {{firstName + " " + lastName}}
</div>

<script>
	var app = angular.module('myApp', []);
	app.controller('myCtrl', function($scope) {
		$scope.firstName = "John";
		$scope.lastName = "Doe";
	});
</script>
	
First Name:
Last Name:
Full Name: {{firstName + " " + lastName}}

  • AngularJS applications are controlled by controllers.
  • AngularJS Controllers is a JavaScript Object, created by a standard JavaScript object constructor.
  • ng-app: Tells AngularJS that the <div> element is the "owner" of an AngularJS application.
  • ng-model: binds the value of the input field to the application variable firstName, lastName.
  • ng-controller: names the controller object.
  • myCtrl function: is a standard JavaScript object constructor. (hàm dựng). Creates two properties (variables) in the scope (firstName and lastName).
  • $scope is the application object (the owner of application variables and functions)
  • AngularJS will invoke myCtrl with a $scope object.(đối tượng)

a) Internal File

Like above code

b) External Files

	
<div ng-app="myApp" ng-controller="personController">
	First Name: <input type="text" ng-model="firstName"><br>
	Last Name: <input type="text" ng-model="lastName"><br>
	Full Name: {{fullName()}}
</div>
<script src="personController.js"></script>
	

In personController.js file

	
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
	$scope.firstName = "John";
	$scope.lastName = "Doe";
});
	

2. Multiple controllers

	
<div ng-app="myApp">
	<div ng-controller="myCtrl">
		{{firstName + ' ' + lastName}}
	</div>
	<div ng-controller="myCtrl2">
		<ul>
  			<li ng-repeat="x in names">
    			{{ x.name + ', ' + x.country }}
  			</li>
		</ul>
	</div>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

app.controller('myCtrl2', function($scope) {
    $scope.names = [
        {name:'Jani',country:'Norway'},
        {name:'Hege',country:'Sweden'},
        {name:'Kai',country:'Denmark'}
    ];
});
</script>
	

Result:

John Doe

  • Jani, Norway
  • Hege, Sweden
  • Kai, Denmark

3. Controller Methods

	
<div ng-app="myApp" ng-controller="personCtrl">
	//First Name: <input type="text" ng-model="firstName">
	//Last Name: <input type="text" ng-model="lastName">
	Full Name: {{fullName()}}
</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.fullName = function() {
        return $scope.firstName + " " + $scope.lastName;
    }
});
</script>
	

Result:

Full Name: John Doe

Không có nhận xét nào:

Đăng nhận xét