<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="js/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<b>请输入姓名:</b><br>
<b>姓:</b><input type="text" ng-model="lastName"><br>
<b>名:</b><input type="text" ng-model="firstName"><br>
<h1>{{ lastName + " " + firstName }}</h1>
<h2>{{ getFullName() }}</h2>
<h2 style='color:red'>{{ fullName }}</h2>
</div>
</body>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.lastName = "";
$scope.firstName = "";
$scope.getFullName =function(){
return $scope.lastName + " " + $scope.firstName;
}
$scope.$watch('lastName', function() {
$scope.fullName = $scope.lastName + " " + $scope.firstName;
});
$scope.$watch('firstName', function() {
$scope.fullName = $scope.lastName + " " + $scope.firstName;
});
});
</script>
</html>