skovmand
skovmand

Reputation: 4422

AngularJS equivalent of v-el

I just found the v-el directive in Vue.js which is a great convenience for DOM-manipulation.

F.ex. I could mark an element with it:

<button v-el="getCustomerButton">Get customers</button>

and in the vue component use for example this.$$.getCustomerButton.focus() to focus the button.

Is there some equivalent to v-el in angularjs?

Regards, Niels.

Upvotes: 1

Views: 277

Answers (1)

Kristiyan Kostadinov
Kristiyan Kostadinov

Reputation: 3712

You can achieve that same result with the following directive:

angular.module('yourModuleNameHere').directive('vEl', [function() {
  return {
    restrict: 'A',
    link: function(scope, element) {
        // do whatever you want with element
        // just element is the jqLite-wrapped object
        // element[0] is the raw DOM node
    }
  };
}]);

Also note that element is wrapped as a jqLite object, if you want the raw DOM node, you can get it with element[0].

In general, when you've applied a directive to an element, you can access that element in the link function.

Upvotes: 1

Related Questions