https://github.com/schnogz/ng-required-params
A simple way to enforce required method parameters in angular
https://github.com/schnogz/ng-required-params
angular default es6 parameters required
Last synced: 4 days ago
JSON representation
A simple way to enforce required method parameters in angular
- Host: GitHub
- URL: https://github.com/schnogz/ng-required-params
- Owner: schnogz
- License: mit
- Created: 2017-03-03T04:00:20.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-05T03:34:08.000Z (about 3 years ago)
- Last Synced: 2026-01-23T22:43:55.650Z (about 1 month ago)
- Topics: angular, default, es6, parameters, required
- Language: JavaScript
- Homepage:
- Size: 162 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ng-required-params
[](https://badge.fury.io/js/ng-required-params)
[](https://david-dm.org/schnogz/ng-required-params.svg)
[](https://github.com/schnogz/ng-required-params/issues)
A tiny angular module that enforces required parameters on function calls by utilizing ES6 default parameters.
## Install
Install using `npm` or `bower`:
```bash
$ npm install ng-required-params --save
```
```bash
$ bower install ng-required-params --save
```
`ng-required-params` has no dependencies other than an ES6 codebase and [Angular](https://angularjs.org/) itself.
## Usage
Simply require `ng-required-params` as a dependency for your app or any angular module.
```javascript
angular
.module('MyApp', ['ng-required-params'])
.controller('MyController', ['$scope', 'ngRequired', function ($scope, ngRequired) {
$scope.incrementCounter = (counter = ngRequired`counter`) => {
console.log('Current count: ' + counter++);
};
$scope.incrementCounter(5); // Logs ==> Current count: 6
$scope.incrementCounter(); // Error ==> Missing parameter: counter
}]);
```
Since required parameters are fundamental and may be often used, it makes sense to make the call to the service as short
and easy-to-type as possible. Therefore it is recommended to use template literal syntax `foo´string´` for calling
ngRequired instead of the normal round brackets syntax `foo('string')`. You may also want to name the imported module as
"ngReg" or "req" instead of "ngRequired" to save even more keystrokes.
### Use with a module loader
If you are using a module loader, you can import the module name when requiring it in angular.
This works with any AMD/UMD/CommonJS module loader.
```javascript
import ngRequiredParamsModule from 'ng-required-params';
angular.module('myModule', [ngRequiredParamsModule.name]);
```