尝试将Parent
和Ancestor
注释添加到TypeScript类型中,以便我这样做:
declare module "angular2/src/core/annotations_impl/visibility"{
function Parent():(target: any) => any;
function Ancestor():(target: any) => any;
}
使用其中一个注释抛出"TypeError: decorator is not a function".
我正在使用alpha 22。
为什么我会收到此错误?
以下是一个示例:
/// <reference path="typings/angular2/angular2.d.ts" />
import {Component,View,bootstrap} from "angular2/angular2"
import {Ancestor,Parent} from "angular2/src/core/annotations_impl/visibility"
@Component({
selector:"c"
})
@View({
template:`<p>{{app.message}}</p>`
})
class C{
app:App;
constructor(@Ancestor() app:App){
this.app = app;
}
}
@Component({
selector:"app"
})
@View({
template:`<c></c>`,
directives:[C]
})
class App{
message = "test";
}
bootstrap(App);
HTML:
<html>
<head>
<title>Angular 2 Quickstart</title>
<script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.89/traceur-runtime.js"></script>
<script src="https://jspm.io/system@0.16.7.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.22/angular2.dev.js"></script>
<body>
<!-- The app component created in app.ts -->
<app></app>
<script>
System.import('app');
</script>
</body>
</html>
答案 0 :(得分:2)
对我而言,这可以通过使用您的原始表单,但在模块中声明它&#39; angular2 / annotations&#39;代替。
抱歉,我没有尝试你的例子,而是Pascal Precht的标签示例。我不确定你是否可以将App视为C的祖先,因为&#34; App被C&#34;取代。
以下适应对我有用。当B和C的定义相反时,我仍然得到你的错误,这看起来很愚蠢。
import {bootstrap, Component, View, For} from 'angular2/angular2';
import {Ancestor,Parent} from 'angular2/annotations';
@Component({
selector:"b"
})
@View({
template:"<content></content>"
})
class B{
message : string = "test";
}
@Component({
selector:"c"
})
@View({
template:`<p>{{b.message}}</p>`
})
class C{
b:B = null;
constructor(@Ancestor() b:B){
this.b = b;
}
}
@Component({
selector:"app"
})
@View({
template:`
<b>
<c></c>
</b>
`,
directives:[B,C]
})
class App{
}
bootstrap(App);