Angular2显示数据示例不起作用

时间:2016-02-15 10:05:09

标签: javascript html angularjs angular angular2-template

我正在关注来自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:未定义角度

1 个答案:

答案 0 :(得分:2)

示例似乎不正确...您应该使用ComponentMetadataViewMetadata对象:

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

这个链接可以给你一些提示: