javascript - Angular 'PersonCtrl' 不是函数,未定义

标签 javascript angularjs typescript

您好,我正在尝试使用 Typescript 构建我的第一个 Angular 应用程序,但我似乎无法正确绑定(bind) Controller 。

这是我的 typescript 代码:

module App {
   var modules: string[] = ["App.Person"];
   angular.module('App', modules)
          .run([]);
} 

    module App.Person {
        angular.module('App.Person', []);
    }

module App.Person {
    angular.module("App.Person")
           .controller('PersonCtrl', PersonCtrl);

    interface IPersonScope extends ng.IScope {
        fullName: string;
    }

    interface IPersonCtrl {

    }

    class PersonCtrl implements IPersonCtrl{
        public $scope: IPersonScope;

        static $inject = ['$scope'];
        constructor($scope: IPersonScope) {
            this.$scope = $scope;
            this.init();            
        }
        init() : void  {
            this.$scope.fullName = 'Justin S.';
        } 
    }
}

这是我的 View 代码:

<article ng-app>
    <section ng-controller="PersonCtrl">
        <p ng-bind="fullName"></p>
    </section>
</article>

编辑 像这样更新我的 html 之后:

   <article ng-app="App">
        <section ng-controller="PersonCtrl">
            <p ng-bind="fullName"></p>
        </section>
    </article>

我开始收到以下错误:

Uncaught Error :[ng:areq] 参数 'fn' 不是函数,未定义

页面上加载了所有相关的javascript文件

最佳答案

需要提供应用名称,根模块AppApp.Person (angular.module('App.Person')) 作为值放入 ng-app :

// this should be the root module name
<article ng-app="App">
// or App.Person
// <article ng-app="App.Person">
    <section ng-controller="PersonCtrl">
        <p ng-bind="fullName"></p>
    </section>
</article>

还要确保具有该定义的文件是页面 <script> 的一部分

关于javascript - Angular 'PersonCtrl' 不是函数,未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29601688/

相关文章:

javascript - 您如何选择处理 Javascript Promise 中的错误?

javascript - 如何将 ng-src 与 ng-repeat 一起用于 IFRAME?

angularjs - Angular Schema 表单 - 覆盖字段的输入类型

javascript - 无法实例化模块时刻选择器

angular - 为 Angular 2 的核心组件提供路由服务

javascript - 类型检查和扩展运算符 (setState)

javascript - 检查无效的查询参数,然后在 (node.js) 中抛出错误

javascript - jQuery mouseleave 无法正常运行

javascript - 为什么 Firefox 和其他浏览器在计算哪个数字更大时表现相反?

html - 在 Angular 中,如何获取具有可扩展内容的 Mat-Table 上的行索引?