@@ -4262,6 +4262,22 @@ describe('$compile', function() {
4262
4262
if ( ! / c h r o m e / i. test ( navigator . userAgent ) ) return ;
4263
4263
/*jshint -W061 */
4264
4264
var controllerCalled = false ;
4265
+ var Controller = eval (
4266
+ "class Foo {\n" +
4267
+ " constructor($scope) {}\n" +
4268
+ " $onInit() { this.check(); }\n" +
4269
+ " check() {\n" +
4270
+ " expect(this.data).toEqualData({\n" +
4271
+ " 'foo': 'bar',\n" +
4272
+ " 'baz': 'biz'\n" +
4273
+ " });\n" +
4274
+ " expect(this.str).toBe('Hello, world!');\n" +
4275
+ " expect(this.fn()).toBe('called!');\n" +
4276
+ " controllerCalled = true;\n" +
4277
+ " }\n" +
4278
+ "}" ) ;
4279
+ spyOn ( Controller . prototype , '$onInit' ) . andCallThrough ( ) ;
4280
+
4265
4281
module ( function ( $compileProvider ) {
4266
4282
$compileProvider . directive ( 'fooDir' , valueFn ( {
4267
4283
template : '<p>isolate</p>' ,
@@ -4270,20 +4286,7 @@ describe('$compile', function() {
4270
4286
'str' : '@dirStr' ,
4271
4287
'fn' : '&dirFn'
4272
4288
} ,
4273
- controller : eval (
4274
- "class Foo {" +
4275
- " constructor($scope) {}" +
4276
- " check() {" +
4277
- " expect(this.data).toEqualData({" +
4278
- " 'foo': 'bar'," +
4279
- " 'baz': 'biz'" +
4280
- " });" +
4281
- " expect(this.str).toBe('Hello, world!');" +
4282
- " expect(this.fn()).toBe('called!');" +
4283
- " controllerCalled = true;" +
4284
- " }" +
4285
- "}"
4286
- ) ,
4289
+ controller : Controller ,
4287
4290
controllerAs : 'test' ,
4288
4291
bindToController : true
4289
4292
} ) ) ;
@@ -4298,7 +4301,7 @@ describe('$compile', function() {
4298
4301
element = $compile ( '<div foo-dir dir-data="remoteData" ' +
4299
4302
'dir-str="Hello, {{whom}}!" ' +
4300
4303
'dir-fn="fn()"></div>' ) ( $rootScope ) ;
4301
- element . data ( '$fooDirController' ) . check ( ) ;
4304
+ expect ( Controller . prototype . $onInit ) . toHaveBeenCalled ( ) ;
4302
4305
expect ( controllerCalled ) . toBe ( true ) ;
4303
4306
} ) ;
4304
4307
/*jshint +W061 */
@@ -4947,6 +4950,32 @@ describe('$compile', function() {
4947
4950
} ) ;
4948
4951
} ) ;
4949
4952
4953
+ it ( 'should call `controller.$onInit`, if provided after all the controllers have been constructed' , function ( ) {
4954
+
4955
+ function check ( ) {
4956
+ /*jshint validthis:true */
4957
+ expect ( this . element . controller ( 'd1' ) . id ) . toEqual ( 1 ) ;
4958
+ expect ( this . element . controller ( 'd2' ) . id ) . toEqual ( 2 ) ;
4959
+ }
4960
+
4961
+ function Controller1 ( $element ) { this . id = 1 ; this . element = $element ; }
4962
+ Controller1 . prototype . $onInit = jasmine . createSpy ( '$onInit' ) . andCallFake ( check ) ;
4963
+
4964
+ function Controller2 ( $element ) { this . id = 2 ; this . element = $element ; }
4965
+ Controller2 . prototype . $onInit = jasmine . createSpy ( '$onInit' ) . andCallFake ( check ) ;
4966
+
4967
+ angular . module ( 'my' , [ ] )
4968
+ . directive ( 'd1' , valueFn ( { controller : Controller1 } ) )
4969
+ . directive ( 'd2' , valueFn ( { controller : Controller2 } ) ) ;
4970
+
4971
+ module ( 'my' ) ;
4972
+ inject ( function ( $compile , $rootScope ) {
4973
+ element = $compile ( '<div d1 d2></div>' ) ( $rootScope ) ;
4974
+ expect ( Controller1 . prototype . $onInit ) . toHaveBeenCalledOnce ( ) ;
4975
+ expect ( Controller2 . prototype . $onInit ) . toHaveBeenCalledOnce ( ) ;
4976
+ } ) ;
4977
+ } ) ;
4978
+
4950
4979
describe ( 'should not overwrite @-bound property each digest when not present' , function ( ) {
4951
4980
it ( 'when creating new scope' , function ( ) {
4952
4981
module ( function ( $compileProvider ) {
0 commit comments