使用带有angular.module()创建角控制器。控制器() [英] Using Angular Controllers created with angular.module().controller()
问题描述
我还是很新的Angular.js和我通过建立我的第一个应用程序的工作。我希望能够做到以下几点:
I am still very new to Angular.js and am working through setting up my first application. I would like to be able to do the following:
angular.module('App.controllers', [])
.controller('home', function () {
$scope.property = true;
}]);
angular.module('App', ['App.controllers'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {templateUrl: 'partials/home.html', controller: home});
}]);
使用此设置将产生以下错误:
Using this setup the following error is generated:
Uncaught ReferenceError: home is not defined from App
我的问题是:我如何注册使用控制器 angular.module.controller()
(或 $ controllerProvider.register()
直接),并在我的应用程序的其他地方使用的注册控制器。
My question is: How can I register controllers using angular.module.controller()
(or $controllerProvider.register()
directly) and use the registered controller elsewhere in my app.
我的动机:我想避免使用任何全球构造函数为我的控制器(因为大多数对angularjs.org使用实例),或者复杂的命名空间。如果我能注册和使用的控制器为单变量名(未然后把在全球范围内),这将是理想的。
My motivation: I would like to avoid using either global constructor functions as my controllers (as most of the examples on angularjs.org use) or complex namespacing. If I can register and use controllers as single variable names (that are not then put in the global scope) that would be ideal.
推荐答案
尝试使用字符串标识符。
Try using a string identifier.
routeProvider.when('/',{templateUrl:'谐音/ home.html做为',控制器:'家'});
当你使用一个文字,它正在寻找一个叫做首页
变量,但这并不在这种情况下存在的。
When you use a literal, it is looking for a variable called home
, but that doesn't exist in this case.
这篇关于使用带有angular.module()创建角控制器。控制器()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!