Improve this Doc View Source

The `ngInit` directive allows you to evaluate an expression in the current scope.

The only appropriate use of `ngInit` is for aliasing special properties of `ngRepeat`, as seen in the demo below. Besides this case, you should use controllers rather than `ngInit` to initialize values on a scope.
Note: If you have assignment in `ngInit` along with `$filter`, make sure you have parenthesis for correct precedence:

## Directive Info

- This directive executes at priority level 450.

## Usage

- as attribute:

      <ANY
        ng-init="">
      ...
      </ANY>

- as CSS class:

      <ANY class="ng-init: ;"> ... </ANY>

### Arguments

Param | Type | Details
--- | --- | ---
ngInit | expression | Expression to eval.

## Example

Edit in Plunker

    <script>
    angular.module('initExample', [])
      .controller('ExampleController', ['$scope', function($scope) {
        $scope.list = [['a', 'b'], ['c', 'd']];
      }]);
    </script>
    <div ng-controller="ExampleController">
    <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>

    it('should alias index positions', function() {
      var elements = element.all(by.css('.example-init'));
      expect(elements.get(0).getText()).toBe('list[ 0 ][ 0 ] = a;');
      expect(elements.get(1).getText()).toBe('list[ 0 ][ 1 ] = b;');
      expect(elements.get(2).getText()).toBe('list[ 1 ][ 0 ] = c;');
      expect(elements.get(3).getText()).toBe('list[ 1 ][ 1 ] = d;');
    });