Angular 1.5.6,$ postLink和typescript

时间:2016-06-05 09:55:34

标签: angularjs hook typescript1.8

我试图在我的指令中使用$ postLink(使用typescript和ng 1.5.6)不幸的是,我真的不明白在哪里使用它。 它应该是指令类主体中名为“$ postLink”的公共函数吗?

以下不起作用:

UICollisionBehavior *collisionBehavior = ...
CGPoint topLeft = ...
CGPoint bottomLeft = ...

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, topLeft.x, topLeft.y);
CGPathAddLineToPoint(path, nil, bottomLeft.x, bottomLeft.y);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath: path];
[collisionBehavior addBoundaryWithIdentifier: @"myID" forPath: path];

2 个答案:

答案 0 :(得分:2)

这就是我使用typescript(这个种子,正是:https://github.com/b091/ts-skeleton)的工作方式:

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

@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");
        }
    }
}

你可以在打字文件中看到角度,IDirective可以采取:

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

OR

interface IDirectiveLinkFn {
    (
        scope: IScope,
        instanceElement: IAugmentedJQuery,
        instanceAttributes: IAttributes,
        controller: {},
        transclude: ITranscludeFunction
    ): void
}

答案 1 :(得分:1)

在组件的控制器中应该是这样的:

class MyCtrl {
  static $inject = ['$element'];
  // If you need the element for DOM manipulation,
  // then inject it as $element.
  // $scope can also be injected.
  constructor($element:ng.IAugmentedJQuery) {}

  // $postLink is different from a directive's link and doesn't have
  // arguments.
  public $postLink() {
     // The attrs can't be injected but instead you can define the
     // bindings of the component.
  }
}