参数' [controllerName]'转换为TypeScript时,不是aNaNunction,未定义

时间:2016-01-25 19:12:20

标签: javascript angularjs typescript

TypeScript入门。我没有使用TypeScript,但在转换时遇到错误。

我有以下控制器:

"use strict"
module app.blogApp {
    class BaHomeController {
        entries: any[];

        static $inject = ["$http"];

        constructor(
            private $http
        ) {
            this.loadEntries();
        }

        private loadEntries() {
            this.$http.get("api/blogEntries").then(function(response) {
                this.entries = response.data;
            });
        }
    }
}

原因是:

"use strict";
var app;
(function (app) {
    var blogApp;
    (function (blogApp) {
        var BaHomeController = (function () {
            function BaHomeController($http) {
                this.$http = $http;
                this.loadEntries();
            }
            BaHomeController.prototype.loadEntries = function () {
                this.$http.get("api/blogEntries").then(function (response) {
                    this.entries = response.data;
                });
            };
            BaHomeController.$inject = ["$http"];
            return BaHomeController;
        })();
    })(blogApp = app.blogApp || (app.blogApp = {}));
})(app || (app = {}));

通过HTML加载:

<section data-ng-controller="BaHomeController as vm"></section>

为什么会产生Angular错误:

  

错误:ng:areq   不好的论点   论证&#39; BaHomeController&#39;不是aNaNunction,未定义

我读到这意味着Angular可能无法找到控制器。使用这种新语法,如何验证控制器名称是否正确?

PS什么是aNaNunction?

1 个答案:

答案 0 :(得分:4)

你在某处错过了这样的一行(假设你还没有注册模块):

angular.module('blogApp', []).controller('BaHomeController', app.blogApp.BaHomeController);