2015-07-20 32 views
0

在Angular中有沒有一種方法可以訪問被點擊的元素類型?我在jQuery中有以下幾個方面,它工作正常,麻煩的是我不知道如何做一個Angular項目中的等價物,我被問到過。在Angular中訪問點擊元素的類型

// Click action 
$(".contactlist").on("click", function(e) { 

    if (!$(e.target).is(':checkbox') && !$(e.target).is("li:nth-child(1)")) { 
     var token = $(this).data('token'); 
     window.location.href = "XYZ.php?page=contacts&token=" + token; 
    } 

}); 

我該如何修改這個Angular事件處理程序來像上面的jQuery一樣工作?

$scope.eventHandler = function(data) { 
     window.location.href = "XYZ.php?page=contacts&token=" + data.token; 
}; 

像這樣的事情????

$scope.eventHandler = function(data) { 
    if (!$(this.target).is(':checkbox') && !$(this.target).is("li:nth-child(1)")) { 
     window.location.href = "XYZ.php?page=contacts&token=" + data.token; 
    } 
}; 

的HTML

<li><input type="checkbox" class="check" data-token="{{data.token}}" name="addselect[]"></li> 
+0

您是否包含jQuery? '$(this.target)'是jQuery,而不是Angular。 – putvande

+0

是包含jQuery庫。 – Rob

回答

0

使用指令與element.bind。

app.directive('clickMe', function() { 
    return function(scope, element, attr, html) { 
    element.bind('mouseup', function() { 
     console.log(JSON.stringify(attr)); 
     }); 
    }; 
    }); 

你的HTML會像這樣:

<input type="checkbox" class="check" data-token="{{data.token}}" click-me name="addselect[]"> 

返回一個複選框如下:

{"$attr":{"type":"type","class":"class","token":"data-token","clickMe":"click-me","name":"name"},"$$element":{"0":{"ng339":7},"length":1},"type":"checkbox","class":"check","token":"","clickMe":"","name":"addselect[]","$$observers":{"token":[]}} 

在該指令,如果你只是想要得到的類型,ATTR。類型將提供它。

checkbox