Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What object type is the instance element parameter pass into the link function of an angular directive?

I am using typescript with angular and trying to create a custom directive. I am attempting to give all my parameters types, but am not sure what type the object that is pass through the $element parameter is. Is it JQuery type? Or some Element type?

In the directive code, I want to use $element with a d3 selector. (i.e. d3.select($element)) Currently the d3 selection statement doesn't work because the $element type is not one that is expected by d3. (I'm using a typescript interface for d3 as well.)

var directiveDefinitionObject : ng.IDirective = {
        restrict: 'E',
        replace: false,
        scope: { data: '=chartData' },
        link: ($scope: ICustomScope, $element: <WHAT_TYPE?>) => {
             d3.select($element);  // d3.select(node)
        }
    };
like image 287
Michelle Avatar asked Feb 14 '14 19:02

Michelle


People also ask

What is Link function is used for in Angular?

link function is basically used to manipulate the DOM( Document Object Model ) element using custom directive. link option in custom directive registers DOM listener and also update the DOM. Watch the live demo or download code from the link given below.

What is link in AngularJS directive?

AngularJS Directive's link key defines link function for the directive. Precisely, using link function, we can define directive's API & functions that can then be used by directive to preform some business logic. The link function is also responsible for registering DOM listeners as well as updating the DOM.

Which directive is used to specify the URL for a element?

templateUrl can also be a function which returns the URL of an HTML template to be loaded and used for the directive. AngularJS will call the templateUrl function with two parameters: the element that the directive was called on, and an attr object associated with that element.

Which of the following is true about the kinds of directives in Angular?

Q 16 - Which of the following is true about ng-model directive? A - ng-model directive binds the values of AngularJS application data to HTML input controls.


1 Answers

$element in the link function of a Directive has the type ng.IAugmentedJQuery. If you include jQuery then you will get the jQuery functions on $element, without jQuery then Angular will provide jqLite. See here for more information.

The link function in ng.IDirective is defined as:

link?: (scope: IScope,
        instanceElement: IAugmentedJQuery,
        instanceAttributes: IAttributes,
        controller: any,
        transclude: ITranscludeFunction
       ) => void;
like image 78
Scott Avatar answered Oct 10 '22 05:10

Scott