为什么这个简单的 angularjs 脚本不起作用?

Why this simple angularjs script is not working?

使用控制器打印数据的简单脚本。 我不知道我哪里做错了.. 代码由index.html、app.js和mail-list.component.js组成。 mail-list.component 用于创建查看和注册控制器的模板。

index.html

<!doctype html>
<html lang="en" ng-app="mailApp">
<head>
<meta charset="utf-8">
<title>Mail System</title>
<link rel="stylesheet" href="css/style.css">
<script src = "angular.min.js"> //angular 2
</script>
<script src="app.js"></script>
<script src="mail-list.component.js"></script>
</head>

<body>
<!--use a custom component to render the mails-->
<mail-list></mail-list>
</body>
</html>

app.js

'use strict';
// Define the 'mailApp' module
angular.module('mailApp', []);

邮件-list.component.js

'use strict';
// Register `mailList` component, along with its associated controller and template

    angular.
      module('mailApp').
        component('mailList',{
          template:
        '<span ng-repeat="mail in $ctrl.mails"> '+
        '<input type="checkbox"><b>{{mail.sender }}:<i> {{mail.subject}}</i></b> {{ mail.message}}</span>',

      controller: function MailListController(){
        this.mails=[
          {
            sender: 'Monu',
            subject: 'Regarding Gate',
            message:'start preparing or gate 2017 exam, dont miss this time'
          },{
                sender: 'Shiv',
                subject: 'College Over',
                message:'B.tech is over, now i am a graduate, hurrey!!'
          },{
            sender: 'Monu',
            subject: 'Regarding bag',
            message:'purchase a new bag for me '
          }

        ];
      }

    });

您似乎缺少标签上的 ng-app 属性。

您正在使用 angular 版本 1.4.9,而组件仅在 angular 1.5.*

中引入

有关详细信息,请参阅 migration guide

正如 Kazimieras 所说,angular 版本不支持 component()

除了他的指南,我还推荐 Todd Motto 的 component polyfill 将组件反向移植到 Angular 1.3+。