javascript - 找不到 AngularJS + Typescript 控制器
问题描述
我正在尝试将打字稿添加到 AngularJS,但出现以下错误
Error: [$controller:ctrlreg] The controller with the name 'MyController' is not` registered
有没有人知道我做错了什么?
我创建了以下类
/// <reference path='references/_all.ts' />
var app = angular.module('shop', []);
class MyController {
constructor($scope: any) {
$scope.message = { title: "Hello World!!" };
};
}
app.controller('MyController', MyController);
和html:
<html>
<head>
...
</head>
<body ng-app="shop">
<div ng-controller="MyController">
{{message.title}}
</div>
</body>
</html>
我正在使用 grunt 来编译它:
/// <reference path='references/_all.ts' />
var app = angular.module('shop', []);
var MyController = (function () {
function MyController($scope) {
$scope.message = { title: "Hello World!!" };
};
return MyController;
})();
app.controller('MyController', MyController);
解决方案
你有应用程序的链接文件吗?
<script src="./app.js"></script>
这是我的解决方案:
var app = angular.module('shop', []);
var MyController = (function () {
function MyController($scope) {
$scope.message = { title: "Hello World!!" };
}
return MyController;
})();
app.controller('MyController', MyController);
<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>
推荐阅读
- android - 创建 buildSrc 和 build.gradle.kts 时出现此错误的新 Android Studio 4.2 Preview
- deep-learning - 不是图像到图像转换的任务如何与 Pix2pix 一起工作?
- talend - Fiddler 未捕获 Talend tRestClient 调用
- python - 异常后需要按键两次 - Python tkinter
- python - Panda Dataframe to.string with a decimal point (,)
- reactjs - 状态未在反应挂钩中更新
- python - 在 Python 中溶解线条
- c++ - C++/WinRT UWP:创建自定义 DataTemplateSelector
- excel - 列中的 VBA SUM 动态范围
- r - 组合居中图例