var app = angular.module('plunker', []).directive('fizzbuzz', function() {
return {
require: 'ngModel',
link: function($scope, $element, $attrs, ctrl) {
// ngModelControllerの$parsesプロパティに検証用の関数を登録します。
ctrl.$parsers.unshift(function(viewValue) {
viewValue = +viewValue;
if (viewValue % 3 === 0 && viewValue % 5 === 0) {
ctrl.$setValidity('fizzbuzz', true);
return viewValue;
} else {
ctrl.$setValidity('fizzbuzz', false);
return undefined;
}
});
}
};
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
<script src="app.js"></script>
</head>
<body>
<form name="form">
<input type="text" name="num" ng-model="num" fizzbuzz>
<span ng-show="form.num.$error.fizzbuzz">3と5の倍数でない。</span>
</form>
</body>
</html>
/* Put your css in here */