Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use element[0] instead of element when creating AngularJS directive with d3?

It seems that you have to use element[0] when creating a directive with D3, for example, like below:

app.directive('firstTry', function () {
    function link(scope, element, attrs) {
        var sampleSVG = d3.select(element[0])
        ...

So, why element[0] but not element? The name element suggests that it is a single object rather than an array, but apparently that's not the case. Another question: what else does this element have?

BTW, any official references about this matter would greatly help.

Thank you very much.

like image 483
JBT Avatar asked Mar 11 '14 21:03

JBT


People also ask

Which directive definition option is used to replace the current element if true?

As the documentation states, 'replace' determines whether the current element is replaced by the directive. The other option is whether it is just added to as a child basically.

What is restrict in AngularJS directive?

Note: When you create a directive, it is restricted to attribute and elements only by default. In order to create directives that are triggered by class name, you need to use the restrict option. The restrict option is typically set to: 'A' - only matches attribute name.


1 Answers

Directives that want to modify the DOM typically use the link option. link takes a function with the following signature, function link(scope, element, attrs) { ... } where:

  • scope is an Angular scope object.
  • element is the jqLite-wrapped element that this directive matches.
  • attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values.

you can find it in documentation here . So to key htmlElement entity - get first member of collection

like image 147
Vasiliy Vanchuk Avatar answered Sep 30 '22 21:09

Vasiliy Vanchuk