|
14 | 14 | - [concat](#concat) |
15 | 15 | - [contains](#contains) |
16 | 16 | - [countBy](#countby) |
| 17 | + - [defaults](#defaults) |
17 | 18 | - [every](#every) |
18 | 19 | - [filterBy](#filterby) |
19 | 20 | - [first](#first) |
@@ -433,6 +434,41 @@ $scope.players = [ |
433 | 434 | Group name: beta, length: 2 |
434 | 435 | Group name: gamma, length: 2 |
435 | 436 | ``` |
| 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 | +``` |
436 | 472 | ###where |
437 | 473 | comparison for each element in a collection to the given properties object,<br/> |
438 | 474 | returning an array of all elements that have equivalent property values. |
|
0 commit comments