<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8" />
<title>AngularJS TIPS</title>
</head>
<body ng-controller="MyController">
<form name="myForm" novalidate>
<label for="name">名前:</label>
<input id="name" name="name" type="text" ng-model="name" />
<button ng-click="onclick()">送信</button>
</form>
<div>{{result}}</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script>
angular.module('myApp', [])
.controller('MyController', ['$scope', '$http', function($scope, $http) {
$scope.onclick = function() {
// 1サーバーに対してHTTP POSTでリクエストを送信
$http({
method: 'POST',
url: 'post.php',
data: { name: $scope.name }
})
// 成功時の処理(ページにあいさつメッセージを反映)
.success(function(data, status, headers, config){
$scope.result = data;
})
// 失敗時の処理(ページにエラーメッセージを反映)
.error(function(data, status, headers, config){
$scope.result = '通信失敗!';
});
};
}]);
</script>
</body>
</html>
|