module
4 methodsangular.module for defining an application module, plus its controller, service, and directive registration.
angular.module(name, requires?) -> moduleGetter/setter for a module. With requires it creates a new module; without requires it retrieves an existing one.
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Module name. |
| requires | Array | Array of dependency module names (omit to get an existing module). |
Returns
module
Example
const app = angular.module('myApp', ['ngRoute']);module.controller(name, constructor)Registers a controller constructor on the module; inject dependencies by parameter name or $inject annotation.
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Controller name. |
| constructor | Function | Constructor function with injectable deps. |
Returns
module
Example
app.controller('UserCtrl', function ($scope, $http) {
$scope.user = { name: 'Sam' };
});module.service(name, constructor)Registers a singleton service instantiated with new; inject dependencies via the constructor.
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Service name. |
| constructor | Function | Constructor function. |
Returns
module
Example
app.service('UserService', function ($http) {
this.list = () => $http.get('/api/users');
});module.directive(name, factory)Registers a directive with a factory function returning a Directive Definition Object (DDO).
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Directive name in camelCase. |
| factory | Function | Factory returning a DDO with restrict, template, link, etc. |
Returns
module
Example
app.directive('hello', function () {
return {
restrict: 'E',
template: '<h1>Hello, {{name}}!</h1>',
scope: { name: '@' },
};
});$scope
4 methodsThe $scope object — the glue between controllers and views — and its digest-cycle methods.
$scope.$watch(watchExpression, listener, deep?)Registers a listener callback invoked whenever the watched expression changes during the digest cycle.
Parameters
| Name | Type | Description |
|---|---|---|
| watchExpression | string | Function | Expression to watch. |
| listener | (newValue, oldValue, scope) => void | Callback on change. |
| deep | boolean | Optional; true for deep (object equality) watch. |
Returns
Function (deregistration)
Example
$scope.$watch('user.name', function (n, o) {
console.log('name changed', o, '->', n);
}, true);$scope.$apply(expr?)Manually triggers the digest cycle, evaluating expr and propagating changes to watchers. Use when updating scope from non-AngularJS code.
Parameters
| Name | Type | Description |
|---|---|---|
| expr | string | Function | Optional expression to evaluate inside the digest. |
Returns
any (result of expr)
Example
setTimeout(function () {
$scope.$apply(function () {
$scope.count = 10;
});
}, 1000);$scope.$on(name, listener)Listens for an event broadcast ($broadcast) or emitted ($emit) on the scope hierarchy.
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Event name. |
| listener | (event, ...args) => void | Handler called on event. |
Returns
Function (deregistration)
Example
$scope.$on('user:login', function (event, user) {
console.log('logged in', user);
});$scope.$broadcast(name, args...)Dispatches an event downward to all child scopes (and their children).
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | Event name. |
| args | any | Optional arguments passed to listeners. |
Returns
event
Example
$scope.$broadcast('item:added', { id: 1 });$http
4 methodsThe $http service for making XMLHttpRequests. Methods return promises resolved with response data, status, and headers.
$http.get(url, config?) -> PromisePerforms an HTTP GET request. The returned promise resolves to a response object with data, status, headers, config.
Parameters
| Name | Type | Description |
|---|---|---|
| url | string | Request URL. |
| config | object | Optional { params, headers, cache, ... }. |
Returns
HttpPromise
Example
$http.get('/api/users/1').then(function (resp) {
console.log(resp.data);
});$http.post(url, data, config?) -> PromisePerforms an HTTP POST request with a request body serialized as JSON by default.
Parameters
| Name | Type | Description |
|---|---|---|
| url | string | Request URL. |
| data | any | Request body. |
| config | object | Optional { headers, params, ... }. |
Returns
HttpPromise
Example
$http.post('/api/users', { name: 'Sam' })
.then(function (resp) { console.log(resp.data); });$http(config) -> PromiseGeneric request method using a config object with method, url, data, params, headers.
Parameters
| Name | Type | Description |
|---|---|---|
| config | object | { method, url, data, params, headers, ... }. |
Returns
HttpPromise
Example
$http({ method: 'PUT', url: '/api/users/1', data: { name: 'Sam2' } })
.then(function (resp) { console.log(resp.status); });promise.then(successCallback, errorCallback?) -> PromiseRegisters callbacks for the response. The success callback receives the full response; the error callback receives the HTTP error.
Parameters
| Name | Type | Description |
|---|---|---|
| successCallback | (response) => any | Called on 2xx response. |
| errorCallback | (response) => any | Called on non-2xx response. |
Returns
Promise
Example
$http.get('/api/users')
.then(
function (resp) { console.log('ok', resp.data); },
function (resp) { console.error('err', resp.status); }
);