|
6 | 6 | * @restrict AC
|
7 | 7 | *
|
8 | 8 | * @description
|
9 |
| - * The `ngInit` directive specifies initialization tasks to be executed |
10 |
| - * before the template enters execution mode during bootstrap. |
| 9 | + * The `ngInit` directive allows you to evaluate an expression in the |
| 10 | + * current scope. |
| 11 | + * |
| 12 | + * <div class="alert alert-error"> |
| 13 | + * The only appropriate use of `ngInit` for aliasing special properties of |
| 14 | + * {@link api/ng.directive:ngRepeat `ngRepeat`}, as seen in the demo bellow. Besides this case, you |
| 15 | + * should use {@link guide/dev_guide.mvc.understanding_controller controllers} rather than `ngInit` |
| 16 | + * to initialize values on a scope. |
| 17 | + * </div> |
11 | 18 | *
|
12 | 19 | * @element ANY
|
13 | 20 | * @param {expression} ngInit {@link guide/expression Expression} to eval.
|
14 | 21 | *
|
15 | 22 | * @example
|
16 | 23 | <doc:example>
|
17 | 24 | <doc:source>
|
18 |
| - <div ng-init="greeting='Hello'; person='World'"> |
19 |
| - {{greeting}} {{person}}! |
20 |
| - </div> |
| 25 | + <script> |
| 26 | + function Ctrl($scope) { |
| 27 | + $scope.list = [['a', 'b'], ['c', 'd']]; |
| 28 | + } |
| 29 | + </script> |
| 30 | + <div ng-controller="Ctrl"> |
| 31 | + <div ng-repeat="innerList in list" ng-init="outerIndex = $index"> |
| 32 | + <div ng-repeat="value in innerList" ng-init="innerIndex = $index"> |
| 33 | + <span class="example-init">list[ {{outerIndex}} ][ {{innerIndex}} ] = {{value}};</span> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | + </div> |
21 | 37 | </doc:source>
|
22 | 38 | <doc:scenario>
|
23 |
| - it('should check greeting', function() { |
24 |
| - expect(binding('greeting')).toBe('Hello'); |
25 |
| - expect(binding('person')).toBe('World'); |
| 39 | + it('should alias index positions', function() { |
| 40 | + expect(element('.example-init').text()) |
| 41 | + .toBe('list[ 0 ][ 0 ] = a;' + |
| 42 | + 'list[ 0 ][ 1 ] = b;' + |
| 43 | + 'list[ 1 ][ 0 ] = c;' + |
| 44 | + 'list[ 1 ][ 1 ] = d;'); |
26 | 45 | });
|
27 | 46 | </doc:scenario>
|
28 | 47 | </doc:example>
|
|
0 commit comments