Angular 1アプリをAngular 2に更新しようとしていて、古いディレクティブの1つに問題があります。フォーカスを追加してクラスを追加する際のAngular2
アイデアは簡単です。入力フィールドにフォーカスが当たったら、クラスを追加し(md-input-focus)、別のクラスを削除する(md-input-wrapper)必要があります。次に、このプロセスは、「ぼやけ」イベント、すなわち、フォーカスが失われたときに逆にされるべきである。
私の古いディレクティブは、単にライン.directive('mdInput',[
'$timeout',
function ($timeout) {
return {
restrict: 'A',
scope: {
ngModel: '='
},
link: function (scope, elem, attrs) {
var $elem = $(elem);
$elem.on('focus', function() {
$elem.closest('.md-input-wrapper').addClass('md-input-focus')
})
.on('blur', function() {
$(this).closest('.md-input-wrapper').removeClass('md-input-focus');
})
}
などを含め
...
私は明らかに私の指示に古典的なスタートを持っていますがが不足している.....スキル
import {Directive, ElementRef, Renderer, Input} from 'angular2/core';
@Directive({
selector: '.mdInput',
})
export class MaterialDesignDirective {
constructor(el: ElementRef, renderer: Renderer) {
// Insert inciteful code here to do the above
}
}
ご協力いただければ幸いです。
UPDATE:(入力要素が集中していた前)
HTMLは次のようになります。
<div class="md-input-wrapper">
<input type="text" class="md-input">
</div>
、その後
<div class="md-input-wrapper md-input-focus">
<input type="text" class="md-input">
</div>
後。
入力要素はフォーカスイベント(したがってディレクティブのターゲット)を受け取ることができる唯一の要素ですが、親の<div>
にはクラスの追加と削除が必要です。
さらにヘルプ
Please see Plunker for help/explanation - 誰かが簡単にBこれはできる
を私はあなたのコードのギュンターのロジックを理解するが、私はそれが非常に収まらないと思う:ちょうど行うクラスを削除します。クラスは親要素に追加する必要があります。 HTML:
親は親コンポーネントではなく親タグですか?この場合、 'ElementRef'が必要です。 Angularに 'nearest()'はありません。それでもそれを使用したいのですか、単に '.parentElement' okですか? –
助けになる場合に指示文を添付するHTMLを表示するために私の質問を更新しました:) –