|
| 1 | +# Implicit matching |
| 2 | +ng-annotate uses static analysis to detect common AngularJS code patterns. |
| 3 | +There are patterns it does not and never will understand and for those you |
| 4 | +should use `"ngInject"` instead, see [README.md](README.md). |
| 5 | + |
| 6 | + |
| 7 | +## Declaration forms |
| 8 | +ng-annotate understands the two common declaration forms: |
| 9 | + |
| 10 | +Long form: |
| 11 | + |
| 12 | +```js |
| 13 | +angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) { |
| 14 | +}); |
| 15 | +``` |
| 16 | + |
| 17 | +Short form: |
| 18 | + |
| 19 | +```js |
| 20 | +myMod.controller("MyCtrl", function($scope, $timeout) { |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +It's not limited to `.controller` of course. It understands `.config`, `.factory`, |
| 25 | +`.directive`, `.filter`, `.run`, `.controller`, `.provider`, `.service`, `.decorator`, |
| 26 | +`.component`, `.animation` and `.invoke`. |
| 27 | + |
| 28 | +For short forms it does not need to see the declaration of `myMod` so you can run it |
| 29 | +on your individual source files without concatenating. If ng-annotate detects a short form |
| 30 | +false positive then you can use the `--regexp` option to limit the module identifier. |
| 31 | +Examples: `--regexp "^myMod$"` (match only `myMod`) or `--regexp "^$"` (ignore short forms). |
| 32 | +You can also use `--regexp` to opt-in for more advanced method callee matching, for |
| 33 | +example `--regexp "^require(.*)$"` to detect and transform |
| 34 | +`require('app-module').controller(..)`. Not using the option is the same as passing |
| 35 | +`--regexp "^[a-zA-Z0-9_\$\.\s]+$"`, which means that the callee can be a (non-unicode) |
| 36 | +identifier (`foo`), possibly with dot notation (`foo.bar`). |
| 37 | + |
| 38 | +ng-annotate understands `angular.module("MyMod", function(dep) ..)` as an alternative to |
| 39 | +`angular.module("MyMod").config(function(dep) ..)`. |
| 40 | + |
| 41 | +ng-annotate understands `this.$get = function($scope) ..` and |
| 42 | +`{.., $get: function($scope) ..}` inside a `provider`. `self` and `that` can be used as |
| 43 | +aliases for `this`. |
| 44 | + |
| 45 | +ng-annotate understands `return {.., controller: function($scope) ..}` inside a |
| 46 | +`directive`. |
| 47 | + |
| 48 | +ng-annotate understands `$provide.decorator("bar", function($scope) ..)`, `$provide.service`, |
| 49 | +`$provide.factory` and `$provide.provider`. |
| 50 | + |
| 51 | +ng-annotate understands `$routeProvider.when("path", { .. })`. |
| 52 | + |
| 53 | +ng-annotate understands `$controllerProvider.register("foo", function($scope) ..)`. |
| 54 | + |
| 55 | +ng-annotate understands `$httpProvider.interceptors.push(function($scope) ..)` and |
| 56 | +`$httpProvider.responseInterceptors.push(function($scope) ..)`. |
| 57 | + |
| 58 | +ng-annotate understands `$injector.invoke(function ..)`. |
| 59 | + |
| 60 | +ng-annotate understands [ui-router](https://github.com/angular-ui/ui-router) (`$stateProvider` and |
| 61 | +`$urlRouterProvider`). |
| 62 | + |
| 63 | +ng-annotate understands `$uibModal.open` (and `$modal.open`) ([angular-ui/bootstrap](http://angular-ui.github.io/bootstrap/)). |
| 64 | + |
| 65 | +ng-annotate understands `$mdDialog.show`, `$mdToast.show` and `$mdBottomSheet.show` |
| 66 | +([angular material design](https://material.angularjs.org/#/api/material.components.dialog/service/$mdDialog)). |
| 67 | + |
| 68 | +ng-annotate understands `myMod.store("MyCtrl", function ..)` |
| 69 | +([flux-angular](https://github.com/christianalfoni/flux-angular)). |
| 70 | + |
| 71 | +ng-annotate understands chaining. |
| 72 | + |
| 73 | +ng-annotate understands IIFE's and attempts to match through them, so |
| 74 | +`(function() { return function($scope) .. })()` works anywhere |
| 75 | +`function($scope) ..` does (for any IIFE args and params). |
| 76 | + |
| 77 | +ng-annotate understands [angular-dashboard-framework](https://github.com/sdorra/angular-dashboard-framework) |
| 78 | +via optional `--enable angular-dashboard-framework`. |
| 79 | + |
| 80 | + |
| 81 | +## Reference-following |
| 82 | +ng-annotate follows references. This works if and only if the referenced declaration is |
| 83 | +a) a function declaration or |
| 84 | +b) a variable declaration with an initializer. |
| 85 | +Modifications to a reference outside of its declaration site are ignored by ng-annotate. |
| 86 | + |
| 87 | +These examples will get annotated: |
| 88 | + |
| 89 | +```js |
| 90 | +function MyCtrl($scope, $timeout) { |
| 91 | +} |
| 92 | +var MyCtrl2 = function($scope) {}; |
| 93 | + |
| 94 | +angular.module("MyMod").controller("MyCtrl", MyCtrl); |
| 95 | +angular.module("MyMod").controller("MyCtrl", MyCtrl2); |
| 96 | +``` |
| 97 | + |
| 98 | + |
0 commit comments