有没有办法在Angular / jqLite中找出元素的类型(在本例中,如果它是输入或文本字段),或者我需要jQuery来执行此操作吗?
代码是:
.directive('localize', ['localize', function(localize) {
return {
link: function(scope, element, attrs) {
scope.$on('languageChange', function() {
var localizedText = localize.localizeText(scope.sourceText);
if (element.is('input, textarea')) { //<--- this right here
element.attr('placeholder', localizedText);
} else {
element.text(localizedText);
}
});
}
};
}])
答案 0 :(得分:2)
您可以使用.tagName,请参阅此处http://jsbin.com/kejeco/1/edit?html,js,console,output
.directive('localize', ['localize', function(localize) {
return {
link: function(scope, element, attrs) {
scope.$on('languageChange', function() {
var localizedText = localize.localizeText(scope.sourceText);
if (element[0].tagName ==="INPUT" || element[0].tagName ==="TEXTAREA") {
element.attr('placeholder', localizedText);
} else {
element.text(localizedText);
}
});
}
};
}])
答案 1 :(得分:1)
虽然这不能解决所有is()
等价物,但它确实提供了一种确定指令链接中元素类型的简单方法:
attrs
还包含名为type
的属性,该属性将包含元素类型:
.directive('localize', ['localize', function(localize) {
return {
link: function(scope, element, attrs) {
scope.$on('languageChange', function() {
var localizedText = localize.localizeText(scope.sourceText);
if ( 'input' == attrs.type || 'textarea' == attrs.type ) {
element.attr('placeholder', localizedText);
} else {
element.text(localizedText);
}
});
}
};
}])