Sadeghbayan
Sadeghbayan

Reputation: 1163

Dynamic attribute with Angular

I'd like to add lang='fa' attribute to all input:text in my form with angular .

with a little condition : if body has class fa all input:text not email type get lang="fa" attribute .

i know how to do it with jquery but i don't know how to do it in angular way .
here is my fiddle with Jquery : Demo
Jquery :

$(document).ready(function(){
        if($("body").hasClass('fa')){
            if('input:text'){
                $('input:text').attr('lang','fa');
            }
        }
    });

Upvotes: 1

Views: 327

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40298

You can add functionality to the input directive (or any directive). This is simply done by declaring it once again. The following will do what you want:

// we assume that the var isFa is already set, e.g as:
var isFa = angular.element(document.body).hasClass('fa');

app.directive('input', function() {
    return {
        restrict: 'E',
        link: function(scope, elem) {
            if( isFa && elem.attr('type') === 'text' ) {
                elem.attr('lang','fa');
            }
        }
    };
});

No need for jQuery. See fiddle: http://jsfiddle.net/b37wdm07/

Upvotes: 2

Related Questions