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)
}
};
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.
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.
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.
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.
$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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With