angular-once =====================
npm install angular-onceangular-once
=====================
One time bindings for AngularJS.
{{ }} or ng-bind directives,
watch under the cover which is being executed every time an angular event loop triggers (for example after $http request, or keypress).
Object.observe and AngularJS
sh
$ bower install angular-once
`
or copy once.js file.
Usage
##### Prerequisites
* reference once.js file
* add once module as adependency to your app (angular.module('yourApp', ['once']))
Lets look at this standard AngularJS code snippet:
`html
`
Now given 100 users (600 watchers), the list is not the only information on the page in most cases.
If user's data needs to be only displayed, and not edited inline, we don't need to set up watchers in a ng-repeat directive, especially as the user goes back and forth within the app many times as the list is being refreshed on each display as controller in angular are transient.
Let's look at the same example using angular-once:
`html
`
Number of watchers? 0 (actually 1 as angular uses a separate watch for the ng-repeat directive itself).
IMPORTANT: Built in angular ng-href and ng-src directives support interpolation ({{ }} notation), angular-once doesn't due to performance reasons (avoid setting up watchers).
API
List below contains comparison of angular-once directives ( one time bindings ) with built in angular directives ( two-way bindings ).
| angular-once | native angular equivalent | example usage |
| ------------- |:-------------:|:-----:|
| once-text="value" | ng-bind or {{ }} ||
| once-html="value" | ng-bind-html ||
| once-src="value" | ng-src ||
| once-href="value" | ng-href ||
| once-title="value" | ng-attr-title ||
| once-alt="value" | ng-attr-alt ||
| once-id="value" | ng-attr-id ||
| once-if="condition" | ng-if ||
| once-class="name:cond" | ng-class ||
| once-style="value" | ng-style ||
| once-show="condition" | ng-show ||
| once-hide="condition" | ng-hide ||
| once once-attr-="value" | ng-attr- ||
#### Important:
One important thing to note is that when using angular-once and bound data is undefined, it creates a watch which waits until the data is available (promise is resolved). Once the promise is resolved, the watch is removed. Reason for that is to be able to use it with data which is not yet available, but still readonly.
In case bound data contains static and dynamic part, for example once-src="'http://placekitten.com/'+ kitty.size" and kitty-size isn't
immediately available, you can use the once-wait-for directive to wait untill kitten.size is fetched, so it will look like:
once-src="'http://placekitten.com/'+ kitty.size" once-wait-for='kitty.size'`