Danil Gholtsman Danil Gholtsman - 4 months ago 20
AngularJS Question

AngularJS select DOM element which belogs directive by tag

Is there a way to select DOM element which belongs directive from I call select command?

As example - I got two directive. Eeach of them got

<input>
inside directive
view.html
.

view1.html

<input></input>


view2.html

<div>other input<div>
<input></input>


I got controllers declared in directives, which I use to handle stuff.

first (called from view1 directive)

$scope.selectView1Input = function(){
var input = angular.element('input');
}


second (called from view2 directive)

$scope.selectView2Input = function(){
var input = angular.element('input');
}


But in bouth situations it selecting the same input filed (I expected his behaviour, because it looks like just dom selector, so)

I don't want to use
id
s. Just simple tags, so, is there any way to do this?

Answer

You can use jQuery or the build-in jqLite to select to element:

app.directive('myDirective', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            var input = element.find('input');
            …
        }
    };
});
Comments