AngularJS でパスワード/メール確認ディレクティブを作成したいのですが、これまでに見たものはすべて、多くの DOM の突っ込みや jQuery のプルに依存しています。可能であれば、 $scope プロパティのみに依存したいと思います。それを行う最善の方法は何ですか?
13736 次
2 に答える
25
この種のディレクティブを実装するための数多くの便利な方法を調べた後、DOM 操作や jQuery を使用せずに実装する方法を見つけました。以下は、その方法を示すプランクです。
それは以下を使用することを含みます:
- 両方の入力フィールドの $scope の ng-model プロパティ
- $parse(expr)(scope) および単純な scope.$watch 式 -- 現在のスコープのコンテキストで、match 属性ディレクティブを追加したコントロールの $modelValue に対して "match" プロパティを評価します。
- 基になるフォームで $invalid プロパティが true の場合、送信ボタンを無効にします。
これが一部の人にとって役立つことを願っています。要点は次のとおりです。
var app = angular.module('app', [], function() {} );
app.directive('match', function($parse) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
scope.$watch(function() {
return $parse(attrs.match)(scope) === ctrl.$modelValue;
}, function(currentValue) {
ctrl.$setValidity('mismatch', currentValue);
});
}
};
});
app.controller('FormController', function ($scope) {
$scope.fields = {
email: '',
emailConfirm: ''
};
$scope.submit = function() {
alert("Submit!");
};
});
次に、HTML で:
<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<link rel="stylesheet" href="style.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="FormController">
<form name='appForm' ng-submit='submit()'>
<div class="control-group">
<label class="control-label required" for="email">Email</label>
<div class="controls">
<input id="email" name="email" ng-model="fields.email"
class="input-xlarge" required="true" type="text" />
<p class="help-block">user@example.com</p>
</div>
</div>
<div class="control-group">
<label class="control-label required" for="emailConfirm">Confirm Email</label>
<div class="controls">
<input name="emailConfirm" ng-model="fields.emailConfirm"
class="input-xlarge" required="true"
type="text" match="fields.email" />
<div ng-show="appForm.emailConfirm.$error.mismatch">
<span class="msg-error">Email and Confirm Email must match.</span>
</div>
</div>
</div>
<button ng-disabled='appForm.$invalid'>Submit</button>
</form>
</body>
</html>
于 2013-07-04T17:24:07.580 に答える
1
これは私にとってはうまくいきます:
指令:
angular.module('myApp').directive('matchValidator', [function() {
return {
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
var pwdWidget = elm.inheritedData('$formController')[attr.matchValidator];
ctrl.$parsers.push(function(value) {
if (value === pwdWidget.$viewValue) {
ctrl.$setValidity('match', true);
return value;
}
if (value && pwdWidget.$viewValue) {
ctrl.$setValidity('match', false);
}
});
pwdWidget.$parsers.push(function(value) {
if (value && ctrl.$viewValue) {
ctrl.$setValidity('match', value === ctrl.$viewValue);
}
return value;
});
}
};
}])
利用方法
<input type="email" ng-model="value1" name="email" required>
<input type="email" ng-model="value2" name="emailConfirm" match-validator="email" required>
表示エラー
<div ng-if="[[yourFormName]].emailConfirm.$error">
<div ng-if="[[yourFormName]].emailConfirm.$error.match">
Email addresses don't match.
</div>
</div>
于 2014-08-19T23:32:21.147 に答える