AngularJSで、フォームが変更されたかどうかの状態を取得します。
【手順】
1.『AngularJS1.5.8のダウンロード方法』の手順で、AngularJS1.5.8をダウンロードしておきます。
2.『index.html』というファイルを作成。
3.『index.html』を以下の様に入力。
<!doctype html>
<html ng-app="mainModule">
<head>
<script src="angular.min.js"></script>
<script>
var mainModule = angular.module('mainModule',[]);
var sampleController = function($scope) {
$scope.getInputItemState = function(inputItem) {
if (inputItem.$pristine) {
return "未変更";
} else if (inputItem.$dirty) {
return "変更あり";
} else {
return "";
}
};
};
mainModule.controller('sampleController', ['$scope', sampleController]);
</script>
<style>
.ng-pristine {
border: solid orange 5px;
}
.ng-dirty {
border: solid blue 5px;
}
form {
padding: 10px;
}
</style>
</head>
<body ng-controller="sampleController">
<form name="sampleForm">
<input type="text" name="textInput" ng-model="textInputValue" /> 状態:{{getInputItemState(sampleForm.textInput)}}<br />
<input type="checkbox" name="checkboxInput" ng-model="checkboxInputValue" /> 状態:{{getInputItemState(sampleForm.checkboxInput)}}<br />
状態:{{getInputItemState(sampleForm)}}
</form>
</body>
</html>
4.ダウンロードした『angular.min.js』と『index.html』を同一ディレクトリに配置します。
5.『index.html』をブラウザで開きます。
6.以下の様に表示されれば成功です。
以上です。



0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。