orszaczky orszaczky -4 years ago 139
AngularJS Question

element.is() equivalent in Angular jqLite

Is there a way to find out the type of an element in Angular/jqLite (in this example if it's an input or text field), or do I need jQuery to do this?

The code is:

.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);
}
});
}
};
}])

Answer Source

You can use .tagName please see here 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);
                }
            });
        }
    };
}])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download