diff options
| author | Brian Ford | 2013-10-10 11:35:30 -0700 | 
|---|---|---|
| committer | Brian Ford | 2013-10-10 13:53:50 -0700 | 
| commit | e819d21fa02f28a64fa8176c132800da2395427e (patch) | |
| tree | 7a2ee541fff2f4840efccdded8e8542f03280df4 | |
| parent | 07272608d8ae39a5dd220cdb5637b1439342d5c3 (diff) | |
| download | angular.js-e819d21fa02f28a64fa8176c132800da2395427e.tar.bz2 | |
docs(ngInit): add note on best practices
| -rw-r--r-- | src/ng/directive/ngInit.js | 35 | 
1 files changed, 27 insertions, 8 deletions
| diff --git a/src/ng/directive/ngInit.js b/src/ng/directive/ngInit.js index 2e2177ef..d8b77c6c 100644 --- a/src/ng/directive/ngInit.js +++ b/src/ng/directive/ngInit.js @@ -6,8 +6,15 @@   * @restrict AC   *   * @description - * The `ngInit` directive specifies initialization tasks to be executed - *  before the template enters execution mode during bootstrap. + * The `ngInit` directive allows you to evaluate an expression in the + * current scope. + * + * <div class="alert alert-error"> + * The only appropriate use of `ngInit` for aliasing special properties of + * {@link api/ng.directive:ngRepeat `ngRepeat`}, as seen in the demo bellow. Besides this case, you + * should use {@link guide/dev_guide.mvc.understanding_controller controllers} rather than `ngInit` + * to initialize values on a scope. + * </div>   *   * @element ANY   * @param {expression} ngInit {@link guide/expression Expression} to eval. @@ -15,14 +22,26 @@   * @example     <doc:example>       <doc:source> -    <div ng-init="greeting='Hello'; person='World'"> -      {{greeting}} {{person}}! -    </div> +   <script> +     function Ctrl($scope) { +       $scope.list = [['a', 'b'], ['c', 'd']]; +     } +   </script> +   <div ng-controller="Ctrl"> +     <div ng-repeat="innerList in list" ng-init="outerIndex = $index"> +       <div ng-repeat="value in innerList" ng-init="innerIndex = $index"> +          <span class="example-init">list[ {{outerIndex}} ][ {{innerIndex}} ] = {{value}};</span> +       </div> +     </div> +   </div>       </doc:source>       <doc:scenario> -       it('should check greeting', function() { -         expect(binding('greeting')).toBe('Hello'); -         expect(binding('person')).toBe('World'); +       it('should alias index positions', function() { +         expect(element('.example-init').text()) +           .toBe('list[ 0 ][ 0 ] = a;' + +                 'list[ 0 ][ 1 ] = b;' + +                 'list[ 1 ][ 0 ] = c;' + +                 'list[ 1 ][ 1 ] = d;');         });       </doc:scenario>     </doc:example> | 
