原文: https://github.com/eoinkelly/notes/blob/master/angular/book-building-web-apps-w-angular/angular.md

---------------------------------------------------------------------------------

Modules

You can create and retrieve modules with the angular.module() function

var modInstance = angular.module('someName', []); // create 'someName' module
var myNameMod = angular.module('someName'); // retrieve a reference to an existing model
  • A module acts as a container for other managed objects
  • The ng-app directive is given the name of a module.
  • Modules can contain any kind of object, not just angular specific ones

In angular you can declarativly express dependencies between modules

  • Pros

    • you can swap collaborators as you need them
    • you are not responsible for creating or destroying (managing life cycle) of collaborators - angular does it for you - this means less code but also less control I guess
      • if you object doesn't create or destroy any of its collaborators, it has less jobs
      • it makes all the collaborators swappable (hence easier to change)
    • it lets you unit test much easier. The ability to swap collaborators is very important for testability

To take advantage of angular DI system, you need to tell it about your code by registering your code in modules.

Angular modules do not store instances of the objects your code needs - they store factories/recipes/constructors that can create these instances

  • The $provide service allows us to register our recipes
  • The recipes are then interpreted by the $injector service (which creates and wires up their dependencies too)
  • objects created from a recipe are called services

NB Angular will only interpret a given recipe once during the applications life cycle - there will only every be one instance of each service object!!!

Consequences

  • there is only one of each controller, filter, directive, service that you register
  • if I want to use Angular DI/modules for my own code, I need to remember that the constructor function I provide will only be run one time during the lifetime of the app.
  • Angular services are singletons!!!
  • ? does this imply that services are not useful for models?

7 ways of registering an object with Angular DI system

  1. moduleInstance.value('name', func)

    • registered by value() cannot have dependencies
    • func must be a constructor
  2. moduleInstance.service()

    • not used very often apparently
    • lets you make services
    • the function you provide must be a constructor (decorates this, no explicit return)
  3. moduleInstance.factory('name', )

    • allows to register any arbitrary object-creating code i.e. the function does not have to be a constructor - it can return anything
    • the function you pass can be used as the module pattern (it can have "private" variables, and return it's public interface)
    • factory is the most common way of registing code with the DI system
  4. moduleInstance.constant('name', value)

    • lets you register a constant that can be declared as a dependency by other modules
    • You could put constants within the function you pass to factory but this is better because
      • they can be swapped out for tests
      • you can re-use the service across many applications
      • con: they don't have default values - the client app has to specify every constant
  5. moduleInstance.provider('name', function)

    • all of the other registration methods are sugar for this one
    • it must return an object that contains a $get property which is a factory function
    • a provider is an object that embeds factory function in it's $get property
    • it can also expose other functions as properties and have hidden data so you can have a default configuration that gets overridden by the functions it exposes.
      • it is the most verbose but most flexibly way of registering
  6. moduleInstance.config(func(fooProvider))

    • note there is no name parameter, only a function
    • allows you to call functions on the provider object before it's $get property is invoked to create the service
    • these functions get run during the config phase (they can change the recipe)
    • gets passed in an instance of a provider (decided by the parameter name)
    • Two ways of registering config functions for angular modules:
    angular.module('my-foo-mod', [], function (myServiceProvider) {// I am **the** config function because I am the third argument (implicit - less obvious)// I am the only one - this is less flexible// the parameter passed in here is a reference to the **provider** that creates the service instances// angular uses its argument parsing magic to know find the provider for 'myService'myServiceProvider.setMaxLen(5);
    });// This form is prefered as it is more explicit and flexible
    angular.module('my-foo-mod', []).config(function () {// I am a config function}).config(function () {// so am I})
    
    • within the config() function we are calling things in the public interface of the provider object (which contains the factory object in $get). This is like changing the recipe, not creating the service using the original recipe and then tweaking it
    • Angular uses it's magic "parse the function text" trick to find the right provider using the naming convention fooProvider (the provider for foo)
  7. moduleInstance.run(func(...))

    • note there is no name parameter, only a function
    • code that should be invoked at the run phase of the module

Function objects registered as modules get an $inject attribute which points to an array of strings representing their dependencies - this is what all the sugar ways of declaring dependencies does.

Inspecting Angular from the console

How do I inspect the angular objects in console?

angular.element($0).scope(); // or just type  $scope with batarang installed

If you change value of some model on $scope and want to have this change reflected in the running application, you need to call $scope.$apply() after making the change.

example here

??? You can almost do it for services:

$('body').injector().get('myMod'); // works iff you Angular is using full jQuery

Module lifecycle

Angular module has two phases

  1. configuration phase

    • all recipes are collected & configured
    • providers can be configured (using modInstance.config()) here
    • config() callbacks are run here
  2. run phase
    • any post-instantiation logic is run here
    • the equivalent of a main method in other languages
    • a module can have multiple run blocks
    • run() callbacks are run here
    • you can use run callbacks to add variables to the $rootScope

Angular modules can be combined into other modules

  • groups of related services can be combined into re-usable modules
  • the top level (application) module can just declare its dependencies on what it needs

Angular has both a services namespace and a module heirarchy. All services are global singletons so the modules (a heirarchy of service creation recipes) eventually creates a flat services namespace.

  • services can declare dependencies on other services, values, constants (they don't care what modules these are in however)
  • modules (each of which can contain multiple services) can declare dependencies on each other

Service visiblity

  • All services are combined into a global namespace so they can all depend on each other no matter what module they are defined in.

    • This means that the module heirarchy is flattened out

      • The module herirarchy is still worthwhile because it helps for organisating code and testing
    • You can override services by defining ones with the same name that are "closer" to the service that needs them.
    • Services defined in modules that are closer to the root of the module heirarchy will override those in child modules
    • There is currently no way of having a service that is private to a module
    • There is currently no way of restricting the visibility of a service

angular 学习理解笔记相关推荐

  1. BERT的学习理解笔记

    BERT的理解 从模型的创新角度看一般,创新不大,但在机器阅读理解顶级水平测试SQuAD1.1中表现出惊人的成绩:全部两个衡量指标上全面超越人类,并且还在11种不同NLP测试中创出最佳成绩,包括将GL ...

  2. Intel SGX初步学习理解笔记(持续更新)

    一些概念 SGX(Software Guard eXtensions)软件保护扩展:是一组CPU指令扩展,能够创造出一个可信执行环境来保护代码和数据,即使使用root 权限也无法访问.通过这个硬件设施 ...

  3. angular学习-2021.5

    angular学习笔记 1.基本概览 模块 Module 组件 Component 指令 Directive 服务 Service 路由 Router 2. 模块Module 2.1 模块的含义 2. ...

  4. 李宏毅强化学习完整笔记!开源项目《LeeDeepRL-Notes》发布

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale开源 核心贡献者:王琦.杨毅远.江季 提起李宏毅老师,熟悉强化学习的读者朋友一 ...

  5. python神经结构二层_《python深度学习》笔记---8.3、神经风格迁移

    <python深度学习>笔记---8.3.神经风格迁移 一.总结 一句话总结: 神经风格迁移是指将参考图像的风格应用于目标图像,同时保留目标图像的内容. 1."神经风格迁移是指将 ...

  6. tensorflow学习函数笔记

    为什么80%的码农都做不了架构师?>>>    [TensorFlow教程资源](https://my.oschina.net/u/3787228/blog/1794868](htt ...

  7. 深度学习-最优化笔记

    深度学习-最优化笔记 作者:杜客 链接:https://zhuanlan.zhihu.com/p/21360434 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 译 ...

  8. 深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器

     深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器 UFLDL即(unsupervised feature learning & deep learning).这是斯坦福网站上的一篇 ...

  9. 学习LOAM笔记——特征点提取与匹配

    学习LOAM笔记--特征点提取与匹配 学习LOAM笔记--特征点提取与匹配 1. 特征点提取 1.1 对激光点按线束分类 1.2 计算激光点曲率 1.3 根据曲率提取特征点 2. 特征点匹配 2.1 ...

最新文章

  1. ICCV 2019 | SPM:单阶段人体姿态估计解决方案
  2. c语言怎么写星星代码,C语言打印星星的问题
  3. 恒大拟36.6亿元出售水晶城项目 企查查显示管理公司曾因违规建设被罚超900万...
  4. 远程登录工具 —— filezilla(FTP vs. SFTP)、xshell、secureCRT
  5. linux 修改git端口号,SSH默认端口更改后使用Git
  6. presto读取oracle,Presto数据接入方式
  7. 【TPshop踩雷篇 — 数据库连接配置失败】
  8. 2011年SSCI收录信息科学与图书馆学学科期刊86种
  9. SQLServer 2012下载及安装教程
  10. 操作系统期末复习题库
  11. 经验分享 | ENVI app store
  12. 【华为编程大赛】投票问题
  13. [HEVC] HEVC学习(五) —— 帧内预测系列之三
  14. 利用Python运行Ansys Apdl
  15. Android无线调试 Wifi连接ADB不用数据线
  16. mongodb关联表查询
  17. winbugs MATLAB,winbugs问题求教:
  18. matlab 图像分割库,图像分割Matlab代码
  19. 全球及中国半导体行业发展方向及项目投资建设分析报告2022-2028年版
  20. 【论文】龙王山小青椒 - 论文写作指导

热门文章

  1. SharePoint 2013 开发——SharePoint Designer 2013工作流
  2. HTTP API 设计指南(基础部分)
  3. 如何計算SDRAM使用頻寬?
  4. SQL SERVER 存储过程学习笔记 (转)
  5. iOS 进阶之底层原理一OC对象原理alloc做了什么
  6. java xml特殊字符处理_dom4j特殊字符处理
  7. thinkphp 5.0.3 rce getshell_关于ThinkPHP的一些渗透方式
  8. vba 定义类_VBA|自定义类型、枚举类型和类模块及其使用
  9. 提高sqlmap爆破效率
  10. Xamarin XAML语言教程构建ControlTemplate控件模板 (四)