简体   繁体   English

带有控制器的Angular 1.5 + TypeScript指令

[英]Angular 1.5 + TypeScript Directive with Controller

I'm trying to create a directive with controller using Angular 1.5 and TypeScript 1.8 but it won't work for me. 我正在尝试使用Angular 1.5和TypeScript 1.8使用控制器创建指令,但它对我不起作用。 I looked at a bunch of examples but I am following those examples to the tee and it still won't work for me. 我看了很多例子,但我在发球台上遵循了这些例子,但它仍然对我不起作用。 Am I cursed? 我被诅咒了吗?

Here is the code: 这是代码:

export class FooController implements angular.IController {
    static $inject = ['myService', '$state'];

    constructor(private myService: Services.MyService, private $state: angular.ui.IStateService) {
    }

    public foo: Services.Dtos.Foo;
    public bar;

    $onInit() {
        if (!this.foo) return;

        this.bar = this.$state.href('app.bar', {id: this.foo.id});
    }
}

export class FooDirective implements angular.IDirective {
    public restrict = 'E';
    public replace = true;
    public scope = {};
    public templateUrl = 'content/templates/foo.template.html';
    public controller = FooController;
    public controllerAs = 'ctrl';
    public bindToController = {
        foo: '<',
    };
}

It errors on FooDirective saying the following: 它在FooDirective显示以下错误:

  1. Class 'FooDirective' incorrectly implements interface 'IDirective'. 类“ FooDirective”错误地实现了接口“ IDirective”。
  2. Types of property 'bindToController' are incompatible. 属性“ bindToController”的类型不兼容。
  3. Type '{ foo: string; 输入'{foo:string; }' is not assignable to type 'boolean | }“不能分配给'boolean | { [boundProperty: string]: string; {[[boundProperty:string]:string; }'. }'。
  4. Type '{ foo: string; 输入'{foo:string; }' is not assignable to type '{ [boundProperty: string]: string; }'不可分配为类型'{{[boundProperty:string]:string; }'. }'。
  5. Index signature is missing in type '{ foo: string; 类型'{foo:string;中缺少索引签名。 }'. }'。

What am I doing wrong? 我究竟做错了什么?

UPDATE 2017/02/15 IDirective looks like this (from angular.d.ts file): 更新2017年2月 15日IDirective看起来像这样(来自angular.d.ts文件):

interface IDirective {
    compile?: IDirectiveCompileFn;
    controller?: string | Injectable<IControllerConstructor>;
    controllerAs?: string;
    /**
     * @deprecated
     * Deprecation warning: although bindings for non-ES6 class controllers are currently bound to this before
     * the controller constructor is called, this use is now deprecated. Please place initialization code that
     * relies upon bindings inside a $onInit method on the controller, instead.
     */
    bindToController?: boolean | {[boundProperty: string]: string};
    link?: IDirectiveLinkFn | IDirectivePrePost;
    multiElement?: boolean;
    priority?: number;
    /**
     * @deprecated
     */
    replace?: boolean;
    require?: string | string[] | {[controller: string]: string};
    restrict?: string;
    scope?: boolean | {[boundProperty: string]: string};
    template?: string | ((tElement: JQuery, tAttrs: IAttributes) => string);
    templateNamespace?: string;
    templateUrl?: string | ((tElement: JQuery, tAttrs: IAttributes) => string);
    terminal?: boolean;
    transclude?: boolean | 'element' | {[slot: string]: string};
}

The directive should be a function that returns a configuration object. 该指令应该是一个返回配置对象的函数。 I would write the directive like this: 我会这样写指令:

export const fooDirective = (): angular.IDirective => {
    return {
        restrict: 'E',
        replace: true,
        scope: {},
        templateUrl: 'content/templates/foo.template.html',
        controller: FooController,
        controllerAs: 'ctrl',
        bindToController: {
            foo: '<'
        }
    };
};

I would also recommend you take a look at the new component API. 我还建议您看看新的组件API。 It greatly simplifies creating new components, and it uses many best practices by default, such as controllerAs . 它极大地简化了创建新组件的过程,并且默认情况下使用了许多最佳实践,例如controllerAs

https://docs.angularjs.org/guide/directive https://docs.angularjs.org/guide/directive
https://docs.angularjs.org/guide/component https://docs.angularjs.org/guide/component

It seems to be the problem with TypeScript definition only accepting boolean. TypeScript定义仅接受布尔值似乎是一个问题。

export class FooDirective implements angular.IDirective {
  public restrict = 'E';
  public replace = true;
  public scope = {};
  public templateUrl = 'content/templates/foo.template.html';
  public controller = FooController;
  public controllerAs = 'ctrl';
  public bindToController:any = {
      foo: '<',
  };
}

This would fix the error, on the other hand it won't typecheck bindToController. 这样可以修复错误,但另一方面,它不会类型检查bindToController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM