您好,我正在尝试使用 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文件
最佳答案
需要提供应用名称,根模块App或App.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/