Angular2:无法解析我的指令的所有参数

时间:2016-09-25 06:23:12

标签: angular angular2-directives angular2-di

我正在编写一个依赖于TemplateRef<any>ViewContainerRef的指令。但是我的指令无法注入这些依赖项。以下是我的所有代码:

main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HelloWorld } from './HelloWorld.directive';

@NgModule({
    imports: [BrowserModule],
    declarations: [AppComponent, HelloWorld],
    bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: ` 
           Hello world!
           <template [lcngHw]="true"><div></div></template>
           `
})
export class AppComponent {
}

HelloWorld.directive.ts

import { Directive } from '@angular/core';
import { Input } from '@angular/core';

import { TemplateRef, ViewContainerRef } from '@angular/core';

@Directive({
    selector: '[lcngHw]'
})
export class HelloWorld{
    constructor(private tf: TemplateRef<any>, private vc: ViewContainerRef){

    }

    @Input()
    set lcngHw(value: boolean) {
        if (value) {
            this.vc.createEmbeddedView(this.tf);
        }
        else {
            this.vc.clear();
        }
    }
}

这是我编译的HelloWorld.directive.js:

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var core_1 = require('@angular/core');
var core_2 = require('@angular/core');
var HelloWorld = (function () {
    function HelloWorld(tf, vc) {
        this.tf = tf;
        this.vc = vc;
    }
    Object.defineProperty(HelloWorld.prototype, "lcngHw", {
        set: function (value) {
            if (value) {
                this.vc.createEmbeddedView(this.tf);
            }
            else {
                this.vc.clear();
            }
        },
        enumerable: true,
        configurable: true
    });
    __decorate([
        core_2.Input()
    ], HelloWorld.prototype, "lcngHw", null);
    HelloWorld = __decorate([
        core_1.Directive({
            selector: '[lcngHw]'
        })
    ], HelloWorld);
    return HelloWorld;
}());
exports.HelloWorld = HelloWorld;

然后我运行我的应用程序,我收到以下错误:

错误:(SystemJS)无法解析HelloWorld的所有参数:(?,?)。

  

错误:无法解析HelloWorld的所有参数:(?,?)。           在CompileMetadataResolver.getDependenciesMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14268:21)           在CompileMetadataResolver.getTypeMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14169:28)           在CompileMetadataResolver.getDirectiveMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:13944:30)           在eval(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14037:51)           at Array.forEach(native)           在CompileMetadataResolver.getNgModuleMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14031:51)           在RuntimeCompiler._compileComponents(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16721:49)           at RuntimeCompiler._compileModuleAndComponents(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16659:39)           在RuntimeCompiler.compileModuleAsync(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16650:23)           在PlatformRef _._ bootstrapModuleWithZone(http://localhost:5000/node_modules/@angular/core/bundles/core.umd.js:6707:29)       评估http://localhost:5000/ViewContainerRefApp/main.js       加载http://localhost:5000/ViewContainerRefApp/main.js时出错           在CompileMetadataResolver.getDependenciesMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14268:21)           在CompileMetadataResolver.getTypeMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14169:28)           在CompileMetadataResolver.getDirectiveMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:13944:30)           在eval(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14037:51)           at Array.forEach(native)           在CompileMetadataResolver.getNgModuleMetadata(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14031:51)           在RuntimeCompiler._compileComponents(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16721:49)           at RuntimeCompiler._compileModuleAndComponents(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16659:39)           在RuntimeCompiler.compileModuleAsync(http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16650:23)           在PlatformRef _._ bootstrapModuleWithZone(http://localhost:5000/node_modules/@angular/core/bundles/core.umd.js:6707:29)       评估http://localhost:5000/ViewContainerRefApp/main.js       加载http://localhost:5000/ViewContainerRefApp/main.js

时出错

我还复制了这个live sample的代码,我也遇到了同样的错误。

TemplateRef<any>ViewContainerRef&#39; s metadata似乎未注册。但我无法找出原因。有人可以帮帮我吗?谢谢。

2 个答案:

答案 0 :(得分:1)

您必须将这些选项之一用于结构指令:

    $conditions = [
        'contain' => [
            'FacilityProjectPerson'=>[
                'FacilityProject'=>[
                    'Project'=>[
                        'fields'=>['Project.name']
                    ],
                ],
            ], 
        ],
    //  'conditions' => [//just in case you need conditions for selecting people
    //      'Person.id' => $this->id, 
    //  ],
        'fields'=>['Person.first_name']
    ];

    $this->Person->find('all',$conditions);

这样您就可以在组件中注入<div *lcngHw="true"></div> <template [lcngHw]="true"><div></div></template> <div template="lcngHw true"></div>

<强> Plunker Example

这也可能有用:

答案 1 :(得分:0)

如果您使用的是tsconfig.json,请添加emitDecoratorMetaData:true

如果您使用的是没有tsConfig.json的Visual Studio,请添加 TypeScriptEmitDecoratorMetadata标记到项目文件 注意:您可能需要更多标签

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptRemoveComments>false</TypeScriptRemoveComments>
    <TypeScriptSourceMap>true</TypeScriptSourceMap>
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptJSXEmit>None</TypeScriptJSXEmit>
    <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
    <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
    <TypeScriptModuleKind>System</TypeScriptModuleKind>
    <TypeScriptModuleResolution>node</TypeScriptModuleResolution>
    <TypeScriptOutFile />
    <TypeScriptOutDir />
    <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
    <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
    <TypeScriptMapRoot />
    <TypeScriptSourceRoot />
    <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
    <TypeScriptEmitDecoratorMetadata>True</TypeScriptEmitDecoratorMetadata>
</PropertyGroup>