- 控制器参数是什么?
该控制器的参数是 依赖关系 ,其被 注入 由AngularJS喷射器的服务。他们可以是任何东西。但是它们通常是将在控制器内部使用的
服务 。
- 带有参数的控制器功能在哪里调用?
控制器,指令,过滤器,服务以及AngularJS中的许多其他东西都是 函数 。但是该框架管理着许多 何时 以及 如何 调用这些功能。
您所说的 与之相关的事物 具有一个名称: dependency ,如上所述。您所说的 魔术 是实际使用的AngularJS
依赖项注入 机制。
当注入器调用这些函数(控制器和其他函数)时,它将读取参数名称(例如:
$scope或
$http或
angularFire)并搜索具有该名称的注册服务,然后在调用该函数时将其作为参数提供。
很简单。您可以通过多种方法来指导您对注射器的“依赖关系”(由注射器管理的参数)。
当您简单地将函数声明为时
function myCtrl($scope) {},注入器将能够$scope从参数名称中找到服务。但是,如果您
最小 化Javascript代码,则注入器将不再能够找到服务,因为参数名称将被修改为较小的字符串,例如“ a”或“ x”。为避免此问题,可以使用
数组符号 指定要注入的服务名称。在这种情况下,您可以这样声明函数:
myCtrl = ['$scope', function($scope){}]您将在AngularJS世界中看到很多 数组符号 用法。现在您开始了解它。你甚至可以注入
$scope,并
angularFire与使用它们
的其他名字 在你的函数(更改名称是 不建议 -在这里这个例子是为了学习目的):
['$scope', 'angularFire',function(skop, af) {}]-这样一来,在函数内部可以使用$范围为“skop”和angularFire为“ af”。该 顺序数组中的服务相匹配的参数的顺序。
另一个例子:
var myController = ['$scope', '$resource', '$timeout', function($scope, $resource, $timeout) { // this controller uses $scope, $resource and $timeout // the parameters are the dependencies to be injected // by AngularJS dependency injection mechanism }];- 在哪里定义angularFire?
在 firebase 模块中 。
如您现在所知,注入器将注入任何东西,只要它已注册并在其记录中可用的“事物” 名称即可 。如果有一个具有该 名称 的“服务” ,则他可以
提供 。
那么,如何构建
name => stuff喷油器使用的清单?
模块 就是答案。一个 模块 只不过是一个列表
name => stuff。在 模块中 ,您可以注册服务,工厂,过滤器,指令等。
仔细看一下官方文档中的Module方法
…几乎所有方法都作为参数接收: name* 和一些“ stuff ”(其中“ stuff”几乎总是一个 函数
,定义了控制器,工厂或指令) )。正是这些“东西”将通过其指定的 名称 注入 。 *
默认情况下,AngularJS服务(例如“ $ timeout”,“ $ http”等)可用,因为 ng模块 已经由框架加载。
对于其他服务,您需要加载/需要 模块 。这就是你做什么 ngRouter , 火力 等..通过加载的 模块 ,其注册的东东都
可以注射 在 你的 模块/应用程序。
让我们看一个分步示例:
// An empty module:var module = angular.module('myModule', []);// Now, adding an "injectable" constant: module.constant('niceStuff', { blip: 'blop', blup: 307 });// We could add a service:module.service('entityManager', ['$http', function($http){ }]);// and so on... if I wanted to use "$resource" instead of "$http"// in the entityManager service above...// ...I would need to require the ngResource when creating the module above,// like this: var module = angular.module('myModule', ['ngResource']);// because "$resource" is not available by default// NOW, anywhere else, since the pre above already ran// I can use those NAMES as dependencies like this:// We are creating another module now:var koolModule = angular.module('km', ['myModule']);// Note that I am requiring the previous module through its registered name// Now, anything I've declared in that module// - just like "ng" (by default) and "firebase" (required) does -// is available for "injection"!!!koolModule.controller('koolController', ['niceStuff', 'entityManager', function(niceStuff, entityManager) { console.log(niceStuff.blip); // 'blop' console.log(niceStuff.blup + 10); // 317 }]);这就是Firebase之类的东西(例如angularFire)变得可用的方式!我们做了什么?首先,我们创建了“
myModule”,并向其中注册了NAMED内容。后来,我们需要为“ koolModule”使用“ myModule”-并且这些名称已经在注射器
name=> stuff列表中可用。
- 如何在参数中链接fbURL
正如我们已经看到的,大多数 模块方法 只是注册事物-给事物 命名 ,以便以后可以通过这些名称 注入 和/或使用它们。
当
module.value('fbURL', 'https://angularjs-projects.firebaseio.com/')被调用时,fbURL (和指定值)登记在
name => stuff列表…在这种情况下,名称为“fbURL”,和值/东西是URL字符串-
但它可以是任何东西!
- 是否可以在其中看到Angular.js提供的所有服务,例如$ location和$ timeout?
是的,API参考:http :
//docs.angularjs.org/api/
注意 模块 左侧导航的组织 方式!首先是 ng
模块,其中包含大量指令,服务,过滤器等。然后,下面是其他模块(ngRoute,ngResource,ngMock等),每个模块都包含自己的服务,适配程序或指令。
感谢您分享这些想法的机会。我喜欢写它们。



