Skip to content

Commit bcd04a2

Browse files
committed
fix(README.md): docs defaults
1 parent 436a4c9 commit bcd04a2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [concat](#concat)
1515
- [contains](#contains)
1616
- [countBy](#countby)
17+
- [defaults](#defaults)
1718
- [every](#every)
1819
- [filterBy](#filterby)
1920
- [first](#first)
@@ -433,6 +434,41 @@ $scope.players = [
433434
Group name: beta, length: 2
434435
Group name: gamma, length: 2
435436
```
437+
###defaults
438+
`defaultsFilter` allows to specify a default fallback value for properties that resolve to undefined.<br/>
439+
**Usage:** `col in collection | defaults: fallback`
440+
```js
441+
$scope.orders = [
442+
{ id:1, destination: { zip: 21908 }, name: 'Ariel M' },
443+
{ id:2, name: 'John F' },
444+
{ id:3, destination: { zip: 45841 } },
445+
{ id:4, destination: { zip: 78612 }, name: 'Danno L' },
446+
];
447+
$scope.fallback = {
448+
name: 'Customer name not available',
449+
destination: { zip: 'Pickup' }
450+
};
451+
```
452+
```html
453+
<li ng-repeat="order in orders | defaults: defaultValue">
454+
<b>id:</b> {{ order.id }},
455+
<b>name:</b> {{ order.name }},
456+
<b>shipping address:</b> {{ order.destination.zip }}
457+
</li>
458+
<!--Results:
459+
* id: 1, name: Ariel M, shipping address: 21908
460+
* id: 2, name: John F, shipping address: Pickup
461+
* id: 3, name: Customer name not available, shipping address: 45841
462+
* id: 4, name: Danno L, shipping address: 78612
463+
```
464+
**Note:** `defaultsFilter` change the source object.<br/>
465+
**Why?** if we not change the source object, it's actually means we gonna return **new** boject(copy operation) **each digest cycle**.<br/>
466+
And it will cause adverse memory and performance implications.<br/>
467+
**How to avoid it?** see below
468+
```js
469+
//We copy it once, and it's really cheaper
470+
$scope.ordersWithFallback = angular.copy($scope.orders);
471+
```
436472
###where
437473
comparison for each element in a collection to the given properties object,<br/>
438474
returning an array of all elements that have equivalent property values.

0 commit comments

Comments
 (0)