无法找到AngularJS + Typescript控制器

时间:2018-04-24 11:43:20

标签: javascript angularjs typescript

我正在尝试将打字稿添加到AngularJS,我收到以下错误

$results = $query->fetchAll();

foreach ($results as $result) {
 //etc
}

有没有人知道我做错了什么?

我创建了以下类

Error: [$controller:ctrlreg] The controller with the name 'MyController' is not` registered

和html:

/// <reference path='references/_all.ts' />
var app = angular.module('shop', []);

class MyController {
    constructor($scope: any) {
        $scope.message = { title: "Hello World!!" };
    };
}

app.controller('MyController', MyController);

我正在使用grunt编译它:

<html>
<head>
...
</head>
<body ng-app="shop">
    <div ng-controller="MyController">
            {{message.title}}
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你有app的链接文件吗?

<script src="./app.js"></script>

这是我的解决方案:

&#13;
&#13;
var app = angular.module('shop', []);

var MyController = (function () {
    function MyController($scope) {
        $scope.message = { title: "Hello World!!" };
    }

    return MyController;
})();

app.controller('MyController', MyController);
&#13;
<head>
  <meta charset="utf-8" />
  <title>AngularJS + Typescript controller cant be found</title>
  <script src="https://code.angularjs.org/1.5.4/angular.js"></script>
  <script src="./app.js"></script>
</head>

<body ng-app="shop">
  <div ng-controller="MyController">
      {{message.title}}
  </div>
</body>
</html>
&#13;
&#13;
&#13;