Angular2 component without view annotation

雨燕双飞 提交于 2019-12-23 03:10:07

问题


I would like to be able to use Angular2 to do client-side databinding on my server-rendered pages (ASP.Net MVC6).

Is it possible to do this without adding a @view template? Instead of defining it inline or creating an external template, I would like to enclose a block of server-rendered HTML with the app element. This was how I did it in Angular1, as it allows me to choose whether to databind on the server-side or on the client-side.

Thanks.


回答1:


You can do something similar, that is probably what you want. Take this example:

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);

The template has a special content <content></content> that represents the ngTransclude directive. This will allow you to add content inside the app tag:

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

Unfortunately this is still poorly undocumented and changing so it's hard to tell about the actual limitations or even if this will remain like this.

Regards.



来源:https://stackoverflow.com/questions/31463911/angular2-component-without-view-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!