Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e819d21

Browse files
committed
docs(ngInit): add note on best practices
1 parent 0727260 commit e819d21

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/ng/directive/ngInit.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,42 @@
66
* @restrict AC
77
*
88
* @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>
1118
*
1219
* @element ANY
1320
* @param {expression} ngInit {@link guide/expression Expression} to eval.
1421
*
1522
* @example
1623
<doc:example>
1724
<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>
2137
</doc:source>
2238
<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;');
2645
});
2746
</doc:scenario>
2847
</doc:example>

0 commit comments

Comments
 (0)