模块和控制器

模块在AngularJS中作为应用组件的容器,包含控制器、服务、过滤器等。它们通过声明方式定义启动过程,使得代码更易理解和复用。模块可以按需加载,有利于单元测试的快速执行和端对端测试的配置调整。$controller服务负责控制器的实例化,可被用于自定义和扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


What is a Module?

You can think of a module as a container for the different parts of your app – controllers, services, filters, directives, etc.

Why?

Most applications have a main method that instantiates and wires together the different parts of the application.

Angular apps don't have a main method. Instead modules declaratively specify how an application should be bootstrapped. There are several advantages to this approach:

  • The declarative process is easier to understand.
  • You can package code as reusable modules.
  • The modules can be loaded in any order (or even in parallel) because modules delay execution.
  • Unit tests only have to load relevant modules, which keeps them fast.
  • End-to-end tests can use modules to override configuration.

什么是Module

  • 大部分的应用都有一个主方法(main)用来实例化,组织,启动应用。
  • AngularJS应用没有主方法,而是用模块来声明应用应该如何启动。
  • 模块应许通过声明的方式来描述应用中的依赖关系,以及如何进行组装和启动

Angular模块

  • 模块是组织业务的一个框框,在一个模块当中定义多个服务器。当引入了一个模块的时候,就可以使用整个模块提供的一种或者多种服务了。
  • AngularJS本身的一个默认模块叫做ng,它提供了 $http, $scope等服务。
  • 服务只是模块听过的多种机制中的一种,其他的指令(directive),过滤器(filter),及其他配置信息。
  • 也可以在已有的模块中新定义一个服务,也可以先新定义一个模块,然后在新模块中定义新服务。
  • 服务是需要显示的声明依赖(引入)关系的,让ng自动的注入。

Module优点

  • 启动过程是声名式的,更容易懂。
  • 在单元测试是不需要加载全部模块的,因此这种方式有助于写单元测试。
  • 可以在特定情况的测试中增加额外的模块,这些模块能更改配置,能帮助进行端对端的测试。
  • 第三方代码可以作为可复用的module打包到Angular中。
  • 模块可以以任何先后或者并行的顺序加载(因为模块的执行本身是延迟的)。

Angular模块

  • angular.module(name[requires],configFn);
  • configFn会在模块初始化时执行,可以在里配置模块的服务
  • configFn @see angular.config()

ng-app
通过ng-app指定对应的模块应用启动
当ng-app=" "这里为空的时候,里面所有的东西就是一个模块(一个全局的ng模块)。
<div ng-app="">
    <div ng-controller="firstController">
        {{name}}
    </div>
</div>

当要调用myApp模块时的用法:

<div ng-app="myApp>
    <div ng-controller="firstController">
        {{name}}
    </div>
</div>

JS页面: (用controller)
var myApp = angular.module('myApp', []);

myApp.controller('firstController', function($scope){
    $scope.name = 'Tom';
})


$controller

  1. - $controllerProvider
  2. - service in module ng

$controller service is responsible for instantiating controllers.

It's just a simple call to $injector, but extracted into a service, so that one can override this service with BC version.

Dependencies

Usage

$controller(constructor, locals);

Arguments

Param Type Details
constructor function()string

If called with a function then it's considered to be the controller constructor function. Otherwise it's considered to be a string which is used to retrieve the controller constructor using the following steps:

  • check if a controller with given name is registered via $controllerProvider
  • check if evaluating the string on the current scope returns a constructor
  • if $controllerProvider#allowGlobals, check window[constructor] on the global window object (not recommended)

    The string can use the controller as property syntax, where the controller instance is published as the specified property on the scope; the scope must be injected into locals param for this to work correctly.

locals Object

Injection locals for Controller.

Returns

Object

Instance of given controller.



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值