角ディレクティブの理解に問題があります。単純な属性を使用してより複雑なhtmlに拡張したいが、このテンプレートのいくつかの部分はパラメータで置き換え可能である。ng-blurとng-focusを使用するための角ディレクティブ
このコードを考える:だから、私は非常にできることを意味します
<form role="form" name="databaseForm">
<input type="text" name="connectionName"
blurred-focused="'databaseForm.connectionName'"
>
</form>
:私は(例えば、「ぼやけ-集中」など)、より簡潔なディレクティブを使用して、それを書きたいと思い
<form role="form" name="databaseForm">
<input type="text" name="connectionName"
ng-focus="databaseForm.connectionName.$focused = true;databaseForm.connectionName.$blurred = false;"
ng-blur="databaseForm.connectionName.$blurred = true;databaseForm.connectionName.$focused = false;"
>
</form>
を他の入力に簡単に再利用してください:
<form role="form" name="databaseForm">
<input type="text" name="username"
blurred-focused="'databaseForm.username'"
>
</form>
この結果、この指令の入力は入力とのユーザーのやりとりに基づいて、$ blurredプロパティと$ focusedプロパティが自動的に適用されます。
ありがとうございます。
更新: 私は、スコープは非絶縁ですMMHunterのバージョンを使用して終了。私は自動的にフォームとフィールドオブジェクトを見つけるためにいくつかのロジックを追加したので、すべてを指定する必要はありませんでした。
マイディレクティブ:
(function() {
"use strict";
angular
.module("app.shared.widgets")
.directive("blurredFocused", blurredFocused);
blurredFocused.$inject = ["_"];
/* @ngInject */
function blurredFocused(_) {
return {
restrict: "A",
priority: -1,
link: function(scope, element, attributes) {
element.on("blur", function() {
var formFieldName = element[0].form.name + "." + element[0].name;
var target = _.get(scope, formFieldName);
scope.$apply(function() {
target.$blurred = true;
target.$focused = false;
});
});
element.on("focus", function() {
var formFieldName = element[0].form.name + "." + element[0].name;
var target = _.get(scope, formFieldName);
scope.$apply(function() {
target.$blurred = false;
target.$focused = true;
});
});
}
};
}
})();
とその使い方の例:あなたが必要
<form role="form" name="databaseForm">
<input type="text" name="connectionName" blurred-focused>
</form>
私は本当に必要なものを理解していません。ぼかし=フォーカスが失われているため、Ng-focusとng-blurは反対の指示です。ぼかしの際に焦点を合わせると、フォーカスコードが起動されます。それについて読む - http://www.w3schools.com/angular/ng_ng-focus.asp、http://www.w3schools.com/angular/ng_ng-blur.asp –
私は基本的に私が持っている構文を凝縮したいそのロジックをどこにでもコピーして貼り付けなくても、最初の例をより再利用可能なものにすることができます。論理がしていることは、質問とは無関係なものです。私は、ぼかしとフォーカスが反対であることを知っていますが、私は、ぼかしと$フォーカスの両方のプロパティを、単一の構文で利用できるようにしたいと思います。 – Casey