【发布时间】:2016-06-14 14:32:34
【问题描述】:
我有一个组件,我想在主引导应用程序之外进行初始化和渲染。
例如,在 react 中,我可以这样做:
var div = document.createElement('div');
React.render(React.createElement(MyComponent, { }), div);
我希望能够在 Angular 2 中使用组件做类似的事情。
另外,我意识到从角度来说,这些组件也可能被视为指令。我不太确定在这种情况下是否应该使用指令或组件术语。
这是我现在正在做的示例代码(我已尝试尽可能减少):
export interface Tweet {
id: number;
text: string;
}
@Component({
selector: 'tweet',
template: `
{{tweet.text}}
`,
inputs: ['tweet']
})
export class TweetComponent {
public tweet: Tweet;
}
// This works when in the context of another component's template like:
<tweet [tweet]="tweet"></tweet>
// code to manually initialize
var tweetEl = document.createElement('tweet');
var body = document.querySelector('body');
body.appendChild(tweetEl);
var comp = new TweetComponent();
comp.tweet = tweet;
bootstrap(TweetComponent, [provide(TweetComponent, {useValue: comp})]);
现在,它给了我以下错误:
异常:类型错误:无法读取未定义的属性“文本” TweetComponent 中的 {{tweet.text}}]
【问题讨论】:
-
来自更多的反应背景,这太疯狂了,这不可能而且容易。
标签: angularjs angular angular2-directives