Angular 1.5.6,$postLink 和打字稿
Angular 1.5.6, $postLink and typescript
我正在尝试在我的指令中使用 $postLink(使用 typescript 和 ng 1.5.6)不幸的是,我真的不知道在哪里使用它。
它应该是指令的 class 主体内名为“$postLink”的 public 函数吗?
以下无效:
public $postLink(scope: ng.IScope , element: ng.IAugmentedJQuery, attrs: ng.IAttributes):void {
}
在你的组件的控制器中应该是这样的:
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.
}
}
这就是我使用打字稿让它工作的方式(这个种子,准确地说: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");
}
}
}
您可以在 angular 的类型文件中看到,IDirective 可以采用:
interface IDirectivePrePost {
pre?: IDirectiveLinkFn;
post?: IDirectiveLinkFn;
}
或
interface IDirectiveLinkFn {
(
scope: IScope,
instanceElement: IAugmentedJQuery,
instanceAttributes: IAttributes,
controller: {},
transclude: ITranscludeFunction
): void
}
我正在尝试在我的指令中使用 $postLink(使用 typescript 和 ng 1.5.6)不幸的是,我真的不知道在哪里使用它。 它应该是指令的 class 主体内名为“$postLink”的 public 函数吗?
以下无效:
public $postLink(scope: ng.IScope , element: ng.IAugmentedJQuery, attrs: ng.IAttributes):void {
}
在你的组件的控制器中应该是这样的:
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.
}
}
这就是我使用打字稿让它工作的方式(这个种子,准确地说: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");
}
}
}
您可以在 angular 的类型文件中看到,IDirective 可以采用:
interface IDirectivePrePost {
pre?: IDirectiveLinkFn;
post?: IDirectiveLinkFn;
}
或
interface IDirectiveLinkFn {
(
scope: IScope,
instanceElement: IAugmentedJQuery,
instanceAttributes: IAttributes,
controller: {},
transclude: ITranscludeFunction
): void
}