我正在关注来自https://angular.io/docs/js/latest/guide/displaying-data.html的Angular2教程,但它对我不起作用。让我知道我在这里失踪了什么。
BTW - 我只使用ES5。
index.html
中的代码 -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Angular2 Demo</title>
<!-- 1. Load libraries -->
<!-- IE required polyfill -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/rxjs/bundles/Rx.umd.js"></script>
<script src="node_modules/angular2/bundles/angular2-all.umd.js"></script>
<script src="app/component.js"></script>
</head>
<body>
<display></display>
</body>
</html>
在component.js
-
function DisplayComponent() {
this.myName = "Alice";
}
DisplayComponent.annotations = [
new angular.ComponentAnnotation({
selector: "display"
}),
new angular.ViewAnnotation({
template:
'<p>My name: {{ myName }}</p>'
})
];
如果我在浏览器中运行idex.html,我会收到错误 -
ReferenceError:未定义角度
答案 0 :(得分:2)
示例似乎不正确...您应该使用ComponentMetadata
和ViewMetadata
对象:
function DisplayComponent() {
this.myName = "Alice";
}
DisplayComponent.annotations = [
new ng.core.ComponentMetadata({
selector: "display"
}),
new ng.core.ViewMetadata({
template:
'<p>My name: {{ myName }}</p>'
})
];
有关详细信息,请参阅此plunkr:https://plnkr.co/edit/biMKXl1WXsgTCxbjwcme?p=preview。
您还可以使用ng.core.Component
对象,如下所述:
var DisplayComponent = ng.core.
Component({
selector: "display"
})
.View({
template:
'<p>My name: {{ myName }}</p>'
})
.Class({
constructor: function() {
this.myName = "Alice";
}
});
请参阅此plunkr:https://plnkr.co/edit/73Hirx9aqnITZCPonltX?p=preview。
这个链接可以给你一些提示: