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

Commit 0e50810

Browse files
Caitlin PotterIgorMinar
Caitlin Potter
authored andcommittedDec 5, 2013
fix(ngInit): evaluate ngInit before ngInclude
The priority of ngInit is adjusted to occur before ngInclude, and after ngController. This enables ngInit to initiallize values in a controller's scope, and also to initiallize values before ngInclude executes. Closes #5167 Closes #5208
1 parent 21e48ab commit 0e50810

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎src/ng/directive/ngInit.js

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* to initialize values on a scope.
1717
* </div>
1818
*
19+
* @priority 450
20+
*
1921
* @element ANY
2022
* @param {expression} ngInit {@link guide/expression Expression} to eval.
2123
*
@@ -47,6 +49,7 @@
4749
</doc:example>
4850
*/
4951
var ngInitDirective = ngDirective({
52+
priority: 450,
5053
compile: function() {
5154
return {
5255
pre: function(scope, element, attrs) {

‎test/ng/directive/ngInitSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,30 @@ describe('ngInit', function() {
1313
element = $compile('<div ng-init="a=123"></div>')($rootScope);
1414
expect($rootScope.a).toEqual(123);
1515
}));
16+
17+
18+
it("should be evaluated before ngInclude", inject(function($rootScope, $templateCache, $compile) {
19+
$templateCache.put('template1.tpl', '<span>1</span>');
20+
$templateCache.put('template2.tpl', '<span>2</span>');
21+
$rootScope.template = 'template1.tpl';
22+
element = $compile('<div><div ng-include="template" ' +
23+
'ng-init="template=\'template2.tpl\'"></div></div>')($rootScope);
24+
$rootScope.$digest();
25+
expect($rootScope.template).toEqual('template2.tpl');
26+
expect(element.find('span').text()).toEqual('2');
27+
}));
28+
29+
30+
it("should be evaluated after ngController", function() {
31+
module(function($controllerProvider) {
32+
$controllerProvider.register('TestCtrl', function($scope) {});
33+
});
34+
inject(function($rootScope, $compile) {
35+
element = $compile('<div><div ng-controller="TestCtrl" ' +
36+
'ng-init="test=123"></div></div>')($rootScope);
37+
$rootScope.$digest();
38+
expect($rootScope.test).toBeUndefined();
39+
expect(element.children('div').scope().test).toEqual(123);
40+
});
41+
});
1642
});

0 commit comments

Comments
 (0)