DanilGholtsman
DanilGholtsman

Reputation: 2374

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 ids. Just simple tags, so, is there any way to do this?

Upvotes: 1

Views: 356

Answers (2)

RWAM
RWAM

Reputation: 7018

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');
            …
        }
    };
});

Upvotes: 2

Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

you can use $event while calling the function from the directive. Call the functions with $event as argument.

ng-click="selectView1Input($event)"

(if you are calling the function on click).

your functions will look like:

first (called from view1 directive)

$scope.selectView1Input = function(event){
     var input = $(event.target);
}

second (called from view2 directive)

$scope.selectView2Input = function(event){
     var input = $(event.target);
}

Upvotes: 0

Related Questions