@@ -647,6 +647,49 @@ describe('$compile', function() {
647
647
} ) ;
648
648
649
649
650
+ describe ( 'template function' , function ( ) {
651
+
652
+ beforeEach ( module ( function ( ) {
653
+ directive ( 'replace' , valueFn ( {
654
+ replace : true ,
655
+ template : function ( e , f ) {
656
+ return '<div class="log" style="width: 10px" high-log>Replace!</div>'
657
+ } ,
658
+ compile : function ( element , attr ) {
659
+ attr . $set ( 'compiled' , 'COMPILED' ) ;
660
+ expect ( element ) . toBe ( attr . $$element ) ;
661
+ }
662
+ } ) ) ;
663
+
664
+ directive ( 'replaceattr' , valueFn ( {
665
+ replace : true ,
666
+ template : function ( e , f ) {
667
+ expect ( e . text ( ) ) . toBe ( 'ignore' ) ;
668
+ return '<div class="log" style="width: 10px" high-log ' + f . myattr + '="123">Replace!</div>'
669
+ } ,
670
+ compile : function ( element , attr ) {
671
+ expect ( element . text ( ) ) . toBe ( 'Replace!' ) ;
672
+ expect ( attr . dynamic ) . toBe ( '123' ) ;
673
+ attr . $set ( 'dynamic' , '456' ) ;
674
+ }
675
+ } ) ) ;
676
+ } ) ) ;
677
+
678
+
679
+ it ( 'should replace element with template returned by function' , inject ( function ( $compile , $rootScope ) {
680
+ element = $compile ( '<div><div replace>ignore</div><div>' ) ( $rootScope ) ;
681
+ expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
682
+ expect ( element . find ( 'div' ) . attr ( 'compiled' ) ) . toBe ( 'COMPILED' ) ;
683
+ } ) ) ;
684
+
685
+ it ( 'should pass element and attributes to template function' , inject ( function ( $compile , $rootScope ) {
686
+ element = $compile ( '<div><div replaceattr myattr="dynamic">ignore</div><div>' ) ( $rootScope ) ;
687
+ expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
688
+ expect ( element . find ( 'div' ) . attr ( 'dynamic' ) ) . toBe ( '456' ) ;
689
+ } ) ) ;
690
+ } ) ;
691
+
692
+
650
693
describe ( 'templateUrl' , function ( ) {
651
694
652
695
beforeEach ( module (
@@ -1214,6 +1257,59 @@ describe('$compile', function() {
1214
1257
} ) ;
1215
1258
} ) ;
1216
1259
1260
+ describe ( 'templateUrl function' , function ( ) {
1261
+
1262
+ beforeEach ( module (
1263
+ function ( ) {
1264
+ directive ( 'hello' , valueFn ( {
1265
+ restrict : 'CAM' , templateUrl : function ( e , t ) {
1266
+ return 'hello.html' ;
1267
+ } ,
1268
+ transclude : true
1269
+ } ) ) ;
1270
+ directive ( 'cau' , valueFn ( {
1271
+ restrict : 'CAM' , templateUrl : function ( e , t ) {
1272
+ expect ( isElement ( e ) ) . toBeTruthy ( ) ;
1273
+ return 'cau' + t . test + '.html' ;
1274
+ }
1275
+ } ) ) ;
1276
+ }
1277
+ ) ) ;
1278
+
1279
+ it ( 'should compile, link and flush the template inline when using functions as templateUrl' , inject (
1280
+ function ( $compile , $templateCache , $rootScope ) {
1281
+ $templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
1282
+ $rootScope . name = 'Elvis' ;
1283
+ element = $compile ( '<div><b hello></b></div>' ) ( $rootScope ) ;
1284
+
1285
+ $rootScope . $digest ( ) ;
1286
+
1287
+ expect ( sortedHtml ( element ) ) . toBeOneOf (
1288
+ '<div><b><span>Hello, Elvis!</span></b></div>' ,
1289
+ '<div><b hello=""><span>Hello, Elvis!</span></b></div>' //ie8
1290
+ ) ;
1291
+ }
1292
+ ) ) ;
1293
+
1294
+ it ( 'should pass element and attributes to the templateUrl function' , inject (
1295
+ function ( $compile , $templateCache , $rootScope ) {
1296
+ $templateCache . put ( 'cau2.html' , '<span>Hey, {{name}}!</span>' ) ;
1297
+ $templateCache . put ( 'cau3.html' , '<span>Say: Hey, {{name}}!</span>' ) ;
1298
+ $rootScope . name = 'me' ;
1299
+ element = $compile ( '<div><b cau test="2"></b><b cau test="3"></b></div>' ) ( $rootScope ) ;
1300
+
1301
+ $rootScope . $digest ( ) ;
1302
+
1303
+ expect ( sortedHtml ( element ) ) . toBeOneOf (
1304
+ '<div><b test="2"><span>Hey, me!</span></b><b test="3">' +
1305
+ '<span>Say: Hey, me!</span></b></div>' ,
1306
+ '<div><b cau="" test="2"><span>Hey, me!</span></b><b cau="" test="3">' +
1307
+ '<span>Say: Hey, me!</span></b></div>' //ie8
1308
+ ) ;
1309
+ }
1310
+ ) ) ;
1311
+ } ) ;
1312
+
1217
1313
1218
1314
describe ( 'scope' , function ( ) {
1219
1315
var iscope ;
0 commit comments