我尝试使用 TypeScript 重写我的 Angular 应用程序,但它不起作用。 MainController 不是一个函数?
我正在使用 Visual Studio 的 TypeScript UMD 编译器,并且尝试在我在本地计算机上设置的 Nodejs/Expressjs 服务器中运行此应用程序。
以下是我在 myApp.js 中定义应用程序的方式:
var myApp = angular.module('myApp', []);
这是 MainController.ts 中的 Controller :
export class MainController {
public USER_DATA: any;
constructor(private $scope: ng.IScope) {
//constructor
}
}
angular.module('myApp')
.controller('MainController', ['$scope', MainController]);
这是 MainController.js 中编译后的 JavaScript 的一小部分:
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports", "MyAppExports"], factory);
}
})(function (require, exports) {
"use strict";
var MainController = (function () {
function MainController($scope) {
var _this = this;
}
return MainController;
}());
exports.MainController = MainController;
angular.module('myApp')
.controller('MainController', ['$scope', MainController]);
});
这是index.html...
<html>
<body ng-app="myApp">
...
<div class="container" ng-controller="MainController">
<a href="#">Notifications <span class="badge">{{USER_DATA.notifications.length}}</span></a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>
<script src="myApp.js"></script>
<script src="MainController.js"></script>
</body>
</html>
为什么我收到 MainController 不是函数的错误?
最佳答案
Why am I getting an error that MainController is not a function?
因为您的文件是 JavaScript 模块,因为 export class MainController {
中的 export
。
有关模块的更多信息:https://basarat.gitbooks.io/typescript/content/docs/project/modules.html
您需要一个模块 bundler 。有几个选项,但我(和 Angular 2 团队)推荐 webpack。快速入门:https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html
关于javascript - 参数 'MainController' 不是函数,未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39337347/