Flavien Volken Flavien Volken - 2 years ago 331
AngularJS Question

Angular 1.5.6, $postLink and typescript

I am trying to use $postLink in my directive (using typescript and ng 1.5.6) Unfortunately, I really do not understand where to use this.
Should it be a public function named "$postLink" within the body of the directive's class ?

The following is not working:

public $postLink(scope: ng.IScope , element: ng.IAugmentedJQuery, attrs: ng.IAttributes):void {


Answer Source

This is how I got it to work using typescript (this seed, precisely: https://github.com/b091/ts-skeleton) :

import {directive} from "../../../decorators/directive";

export class MyDirective implements ng.IDirective {
    public restrict: string = "A";

    public link: ng.IDirectivePrePost = {
        pre: ($scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes): void => {
            console.log("pre() being called");
        post: ($scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes): void => {
            console.log("post() being called");

You can see in the typings file for angular, that IDirective can take either:

interface IDirectivePrePost {
    pre?: IDirectiveLinkFn;
    post?: IDirectiveLinkFn;


interface IDirectiveLinkFn {
        scope: IScope,
        instanceElement: IAugmentedJQuery,
        instanceAttributes: IAttributes,
        controller: {},
        transclude: ITranscludeFunction
    ): void
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download