IT/앵귤러js

AngularJS Controllers - Controller Methods

조원태 2017. 8. 6. 14:28
반응형

AngularJS Controllers - Controller Methods 

Controller Methods(컨트롤러 메소드) 

위 예제는 lastName과 firstName의 두 가지 속성을 가진 컨트롤러 객체를 보여줍니다. 

컨트롤러는 메소드 (변수를 함수로)를 가질 수도 있습니다 : 

<div ng-app="myApp" ng-controller="personCtrl"> 

First Name: <input type="text" ng-model="firstName"><br> 
Last Name: <input type="text" ng-model="lastName"><br> 
<br> 
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> 


전체소스 
<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="personCtrl"> 

First Name: <input type="text" ng-model="firstName"><br> 
Last Name: <input type="text" ng-model="lastName"><br> 
<br> 
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> 

</body> 
</html>

반응형