如何在AngularJS中注入控制器?

时间:2014-07-18 04:58:05

标签: javascript angularjs angularjs-scope angularjs-module angularjs-injector

如何让此设置正常工作?

app.js

var app = angular.module('app', ['ui.router', 'app.controllers']);
/* FooCtrl isn't available here, why not? */

controllers.js

var controllers = angular.module('app.controllers', []);
controllers.controller('FooCtrl', function ($scope, $log) {});

的index.html

<script src="app.js"></script>
<script src="controllers.js"></script>
<!-- also tried swapping this order -->

http://plnkr.co/edit/rclYeiCepcMwS3CCPl6D?p=preview

1 个答案:

答案 0 :(得分:0)

您的控制器不是全局构造函数,而是在模块内部定义,因此您需要使用语法。

controller: 'FooCtrl'

而不是

controller: FooCtrl