【发布时间】:2017-01-13 05:21:18
【问题描述】:
我尝试使用 TypeScript 重新编写我的 Angular 应用程序,但它不起作用。 MainController 不是函数?
我正在使用 Visual Studio 的 TypeScript UMD 编译器,并尝试在我在本地计算机上设置的 Nodejs/Expressjs 服务器中运行此应用程序。
这是我在 myApp.js 中定义我的应用程序的方式:
var myApp = angular.module('myApp', []);
这是 MainController.ts 中的控制器:
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 不是函数的错误?
【问题讨论】:
标签: javascript angularjs node.js visual-studio typescript