フォーム フィールドのバリデータを作成したいと考えています。
私の場合、非表示の編集フォームとユーザー名を表示するリストを含むページがあります。リスト内のユーザー名の 1 つをクリックすると、check-unique ディレクティブを含む編集フィールドが下に表示されます。
編集フォームを開くとすぐに、パラメーター値をディレクティブに解析したいと考えています。ただし、ページは既にコンパイルされており、値はページのロード中にのみディレクティブに解析されるため、ディレクティブではもちろん更新されません。したがって、ユーザー名を属性値として解析したいのに、 attr.checkUnique 値が空です。
これが私のフィドルです。 http://jsfiddle.net/charms/r3ajt/28/
「openEdit」メソッドの実行中に新しいパラメータを引き継ぐためにディレクティブを何らかの形で更新できるかどうか誰かが知っていますか?
または、この問題を克服するための他の賢い方法はありますか? どういうわけか私はここで立ち往生しています。
HTML
<div ng-app="myApp" ng-controller="HandleCtrl">
<div ng-repeat="u in users">
{{u.username}}<button ng-click="openEdit({{u.id}})">Edit</button><br/>
</div>
<form ng-show="showNew" novalidate>
<input type="text" name="username"/>
</form>
<form ng-show="showEdit" novalidate>
<input type="text" name="username" ng-model="user.username" check-unique="{{user.username}}"/>
</form>
</div>
AngularJS
angular.module('myApp', [])
.controller('HandleCtrl', ['$scope',function($scope) {
$scope.showNew = false;
$scope.showEdit = false;
$scope.user = {};
$scope.users = [];
$scope.users[0] = {username:'matt', id:1};
$scope.users[1] = {username:'bob', id:2};
$scope.users[2] = {username:'tom', id:3};
$scope.openEdit = function(id) {
$scope.showEdit = true;
$scope.user = $scope.users[id-1];
};
}])
.directive('checkUnique', [function() {
return {
require: 'ngModel',
restrict: 'A',
scope: false,
link: function(scope, elem, attr, ctrl) {
console.log("I want username but value is empty: " + attr.checkUnique);
}
};
}]);