angular - 带有 ES5 的 Angular 2 中的管道+指令 : No Directive annotation found on constructor

标签 angular

我有一个非常简单的管道:

app.DisplayKeystrokePipe = ng.core
    .Pipe({
        name: "displayKeystroke"
    })
    .Class({
        transform: function() {

        }
    });

以及更复杂的组件/指令:

app.DropDownCmp = ng.core
    .Component({
        selector: "dropdown",
        templateUrl: "dropdown.html",
        inputs: [
            'list',
            'selected'
        ],
        outputs: [
            'selectedChange'
        ]
    })
    .Class({
        constructor: function() {
            this.selectedChange = new ng.core.EventEmitter();
            this.display = "Hello";
            this.onClickHandler = function(event) {
                if(this.listVisible && !$(event.target).is(".dropdown-display") && !$(event.target).parents(".dropdown-display").length > 0) {
                    this.listVisible = false;
                }
            }.bind(this);
        },
        isSelected: function(id) {
            return id == this.selected;
        },
        show: function() {
            this.listVisible = true;
        },
        select: function(id) {
            this.selected = id;
            this.selectedChange.next(id);
        },
        ngOnInit: function() {
            document.addEventListener('click', this.onClickHandler)
        },
        ngOnDestroy: function() {
            document.removeEventListener('click', this.onClickHandler);
        },
        ngOnChanges: function(changes) {
            this.display = this.list[this.selected];
        },
    });

现在我尝试在一个模板中使用这两个东西:

app.SomeComponent = ng.core
    .Component({
        template: `<dropdown [list]="someList" [(selected)]="currentEntry"></dropdown><br>{{1 | displayKeystroke}}`,
        directives: [app.DropDownCmp],
        pipes: [app.DisplayKeystrokePipe]
    })
    .Class({
        constructor: [ng.router.RouteData, app.LinkService, function(rd, service) {
            service.setPath(rd.data.catName, rd.data.name);
            this.someList = ['Entry1', 'Entry2', 'Entry3'];
            this.currentEntry = 0;
        }]
    });

但是,我收到错误消息:异常:在构造函数上找不到指令注释

当我只在模板中使用该指令时,它可以工作:

template: `<dropdown [list]="someList" [(selected)]="currentEntry"></dropdown>`

当我只使用管道时也是如此:

template: `{{1 | displayKeystroke}}`

只有当我同时使用两者时,才会出现此错误。

我做错了什么?

编辑:Plunker:http://plnkr.co/edit/A0hnvvVC6oxZBqIE38Eu?p=preview

最佳答案

我想我发现了问题,在你的管道中有这一行

constructor: function constructor() {}

该行使其失败,如何以及为什么?遗憾的是我不能确定。但是,如果您更改它以匹配类名,或者简单地使用 function() {} 就可以了。

app.DisplayKeystrokePipe = ng.core.Pipe({
    name: "displayKeystroke"
}).Class({

    // Match class name
    constructor: function DisplayKeystrokePipe () {},

    // or simply using function() {}
    //constructor: function() {},

    transform: function() {
        return "FIRED";
    }
});

奇怪的是,对于也有构造函数:function constructor() {}的指令,这种情况不会发生。因此,我只是坚持使用 constructor: function() {} 以避免出现问题。

这是您的plnkr工作。

关于angular - 带有 ES5 的 Angular 2 中的管道+指令 : No Directive annotation found on constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116759/

相关文章:

javascript - 如何设置Angular 2.0的环境?

node.js - 以 Angular 创建新项目时出错

angular - mat-expansion-panel 主体不适用于 ngFor

html - Angular Animations 滑入不起作用,只是出现

angular - 在现有的 Angular 应用程序上添加 Nrwl Nx 后 ng g app 在错误的目录中添加应用程序

angular - 如何将 Observable<any> 转换为 array[]

html - 为什么在使用 *ngFor 后滑动 slider 不播放

javascript - 尝试从 "observable' s 链获取可观察性时出现错误”

angular - 错误 TS1238 : Unable to resolve signature of class decorator when called as an expression. Angular

angular - 我如何将数据从我的父组件传递到 Angular 中的子组件?