New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(ngInit): add note on best practices #4366

wants to merge 1 commit into
base: master
Jump to file or symbol
Failed to load files and symbols.
+27 −8
Diff settings


Just for now

@@ -6,23 +6,42 @@
* @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.
* @example
<div ng-init="greeting='Hello'; person='World'">
{{greeting}} {{person}}!
function Ctrl($scope) {
$scope.list = [['a', 'b'], ['c', 'd']];
<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>
it('should check greeting', function() {
it('should alias index positions', function() {
.toBe('list[ 0 ][ 0 ] = a;' +
'list[ 0 ][ 1 ] = b;' +
'list[ 1 ][ 0 ] = c;' +
'list[ 1 ][ 1 ] = d;');
ProTip! Use n and p to navigate between commits in a pull request.