没有视图注释的 Angular2 组件

Angular2 component without view annotation

我希望能够使用 Angular2 在我的服务器呈现页面 (ASP.Net MVC6) 上进行客户端数据绑定。

是否可以在不添加@view 模板的情况下执行此操作?我不想内联定义它或创建外部模板,而是想用 app 元素包含一个服务器呈现的 HTML 块。这就是我在 Angular1 中的做法,因为它允许我选择是在服务器端还是在客户端进行数据绑定。

谢谢。

您可以做类似的事情,这可能就是您想要的。举个例子:

import {Component, View, bootstrap} from "angular2/angular2";

@Component({
    selector: "app"
})
@View({
    template:  "<content></content>"
})
export class App {
    public name: string;

    constructor() {
        this.name = "MyName";
    }
}
bootstrap(App);

模板有特殊内容 <content></content> 表示 ngTransclude 指令。这将允许您在应用标签内添加内容:

<app>
    <h1>Hello World</h1>
</app> 

不幸的是,这仍然没有记录在案并且正在改变,所以很难说出实际的限制,或者即使它会保持这样。

此致。