wo
watch-once
npm i watch-once
wo

watch-once

An angular module that adds a "Once" counterpart to angular's $watch functions which will only call the listener when the expression is defined and then immediately stop the watch.

by Kent C. Dodds

2.0.0 (see all)License:MITTypeScript:Not Found
npm i watch-once
Readme

$watchOnce

Bower version Code Climate Build Status

NPM

This module adds $watchOnce, $watchGroupOnce, and $watchCollectionOnce to angular scopes. Each of these have the same signature as their angular counterparts. The difference is the listener you provide is only called once, and only when the value is defined. Note that for $watchGroupOnce, it will only be called when every expression's value is defined. Also, $watchGroupOnce is only available in Angular >= 1.3. Once your listener has been called, the watcher stops itself from watching.

To Get

Get the code on GitHub: index.js | index.min.js

Or by using bower or npm:

bower|npm install watch-once

To Use

Include it on your page with a script tag (or require it in using commonjs or amd) and add it to your dependencies:

// CommonJS
angular.module('app', [require('watch-once')]);

// AMD
define(['path/to/watch-once/index.js'], function(watchOnce) {
  angular.module('app', [watchOnce]);
});

// global
angular.module('app', ['watchOnce']);



// Then you can do this:

$scope.$watchOnce('expression', function(value) {
  console.log(value); // <-- will only print once and will never be undefined.
});

// it works in link functions as well (other solutions don't do this).

// you can also use $watchGroupOnce and $watchCollectionOnce

See the test-runner.js and the demo page (which runs the test runner).

Angular compatibility

This is compatible with Angular >= 1.0.0. Note, that older versions only had $watch so only $watchOnce is supported for the older versions.

Downloads/wk

29

GitHub Stars

10

LAST COMMIT

7yrs ago

MAINTAINERS

1

CONTRIBUTORS

3

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
2.0.0
latest
7yrs ago
No alternatives found
No tutorials found
Add a tutorial