我正在更新Angular 2的Angular 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 - 將是巨大的,如果有人可以幫助
我理解你的代碼岡特的邏輯,但我認爲它不太適合。該類應該被添加到父元素。 HTML:
好的,但父母不是父母的組件,但父母的標籤?在這種情況下,你需要'ElementRef'。在Angular中沒有'最接近的()'。你仍然想使用它,或者只是'.parentElement'好嗎? –
我已更新我的問題,以顯示指令應附加到的HTML,以防情況:) –