Skip to content

Commit c3e87ad

Browse files
mlnkwchristopherthielen
authored andcommitted
fix(viewDirective): add check for componentProvider, avoid extra trigger for $onInit (fixing #3735) (#3779)
1 parent 74772bd commit c3e87ad

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/directives/viewDirective.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ function registerControllerCallbacks(
465465
cfg: Ng1ViewConfig
466466
) {
467467
// Call $onInit() ASAP
468-
if (isFunction(controllerInstance.$onInit) && !(cfg.viewDecl.component && hasComponentImpl)) {
468+
if (isFunction(controllerInstance.$onInit) && !((cfg.viewDecl.component || cfg.viewDecl.componentProvider) && hasComponentImpl)) {
469469
controllerInstance.$onInit();
470470
}
471471

test/viewDirectiveSpec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,28 @@ describe('angular 1.5+ style .component()', function() {
13341334
expect(log).toBe('onInit;');
13351335
});
13361336

1337+
it('should only call $onInit() once with componentProvider', function() {
1338+
$stateProvider.state('route2cmp', {
1339+
componentProvider: () => 'ngComponent',
1340+
resolve: {
1341+
data: function() {
1342+
return 'DATA!';
1343+
},
1344+
},
1345+
});
1346+
1347+
const $state = svcs.$state,
1348+
$httpBackend = svcs.$httpBackend,
1349+
$q = svcs.$q;
1350+
1351+
$httpBackend.expectGET('/comp_tpl.html').respond('-{{ $ctrl.data }}-');
1352+
$state.transitionTo('route2cmp');
1353+
$q.flush();
1354+
$httpBackend.flush();
1355+
1356+
expect(log).toBe('onInit;');
1357+
});
1358+
13371359
it('should supply resolve data to "<", "=", "@" bindings', function() {
13381360
$stateProvider.state('bindingtypes', {
13391361
component: 'bindingTypes',

0 commit comments

Comments
 (0)