HTML:
<html ng-app="app">
<div class="container" style="margin-top: 30px">
<input type="text" ng-model="newName" key-filter/>
<input type="text" ng-model="newName" key-filter/>
<input type="text" ng-model="newName" key-filter/>
<input type="text" ng-model="newName" key-filter/>
</div>
</html>
JS:
var app = angular.module('app', []);
app.directive('keyFilter', function() {
var pattern = /([\s !$%^&*()_+|~=`{}\[\]:";'<>?,.\/])/;
function link(scope) {
scope.$watch('model', function() {
if(scope.model === undefined)
return
if(pattern.test(scope.model)) {
scope.model = scope.model.replace(pattern, '');
Materialize.toast('Denied symbol', 4000, 'rounded');
}
});
}
return {
restrict: 'A',
scope: {
model: '=ngModel'
},
link: link
}
});
同じモデルにバインドされている多くの入力があり、ユーザー入力をフィルタリングしています。ユーザーが拒否されたキーを押したときに、トーストを表示して、このシンボルを使用できないことを通知したかったのですが、トーストの数は同じです同じモデルにバインドする入力の数に。私は1つのモデルでのみ作業していると思いました。
例: http://codepen.io/anon/pen/XbLjVY?editors=101
どうすればそれを修正できますか、またはロジックがどのように機能するかを変更できます
ps角初心者