@@ -625,6 +625,50 @@ describe('$compile', function() {
625
625
} ) ;
626
626
627
627
628
+ describe ( 'template function' , function ( ) {
629
+
630
+ beforeEach ( module ( function ( ) {
631
+ directive ( 'replace' , valueFn ( {
632
+ restrict : 'CAM' ,
633
+ replace : true ,
634
+ template : function ( e , f ) {
635
+ return '<div class="log" style="width: 10px" high-log>Replace!</div>'
636
+ } ,
637
+ compile : function ( element , attr ) {
638
+ attr . $set ( 'compiled' , 'COMPILED' ) ;
639
+ expect ( element ) . toBe ( attr . $$element ) ;
640
+ }
641
+ } ) ) ;
642
+
643
+ directive ( 'replaceattr' , valueFn ( {
644
+ restrict : 'CAM' ,
645
+ replace : true ,
646
+ template : function ( e , f ) {
647
+ expect ( isElement ( e ) ) . toBeTruthy ( ) ;
648
+ return '<div class="log" style="width: 10px" high-log ' + f . myattr + '="123">Replace!</div>'
649
+ } ,
650
+ compile : function ( element , attr ) {
651
+ expect ( attr . dynamic ) . toBe ( '123' ) ;
652
+ attr . $set ( 'dynamic' , '456' ) ;
653
+ }
654
+ } ) ) ;
655
+ } ) ) ;
656
+
657
+
658
+ it ( 'should replace element with template returned by function' , inject ( function ( $compile , $rootScope ) {
659
+ element = $compile ( '<div><div replace>ignore</div><div>' ) ( $rootScope ) ;
660
+ expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
661
+ expect ( element . find ( 'div' ) . attr ( 'compiled' ) ) . toEqual ( 'COMPILED' ) ;
662
+ } ) ) ;
663
+
664
+ it ( 'should pass element and attributes to template function' , inject ( function ( $compile , $rootScope ) {
665
+ element = $compile ( '<div><div replaceattr myattr="dynamic">ignore</div><div>' ) ( $rootScope ) ;
666
+ expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
667
+ expect ( element . find ( 'div' ) . attr ( 'dynamic' ) ) . toEqual ( '456' ) ;
668
+ } ) ) ;
669
+ } ) ;
670
+
671
+
628
672
describe ( 'templateUrl' , function ( ) {
629
673
630
674
beforeEach ( module (
@@ -1155,6 +1199,54 @@ describe('$compile', function() {
1155
1199
} ) ;
1156
1200
} ) ;
1157
1201
1202
+ describe ( 'templateUrl function' , function ( ) {
1203
+
1204
+ beforeEach ( module (
1205
+ function ( ) {
1206
+ directive ( 'hello' , valueFn ( {
1207
+ restrict : 'CAM' , templateUrl : function ( e , t ) {
1208
+ return 'hello.html' ;
1209
+ } ,
1210
+ transclude : true
1211
+ } ) ) ;
1212
+ directive ( 'cau' , valueFn ( {
1213
+ restrict : 'CAM' , templateUrl : function ( e , t ) {
1214
+ expect ( isElement ( e ) ) . toBeTruthy ( ) ;
1215
+ return 'cau' + t . test + '.html' ;
1216
+ }
1217
+ } ) ) ;
1218
+ }
1219
+ ) ) ;
1220
+
1221
+ it ( 'should compile, link and flush the template inline when using functions as templateUrl' , inject (
1222
+ function ( $compile , $templateCache , $rootScope ) {
1223
+ $templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
1224
+ $rootScope . name = 'Elvis' ;
1225
+ element = $compile ( '<div><b hello></b></div>' ) ( $rootScope ) ;
1226
+
1227
+ $rootScope . $digest ( ) ;
1228
+
1229
+ expect ( sortedHtml ( element ) ) .
1230
+ toEqual ( '<div><b><span>Hello, Elvis!</span></b></div>' ) ;
1231
+ }
1232
+ ) ) ;
1233
+
1234
+ it ( 'should pass element and attributes to the templateUrl function' , inject (
1235
+ function ( $compile , $templateCache , $rootScope ) {
1236
+ $templateCache . put ( 'cau2.html' , '<span>Hey, {{name}}!</span>' ) ;
1237
+ $templateCache . put ( 'cau3.html' , '<span>Say: Hey, {{name}}!</span>' ) ;
1238
+ $rootScope . name = 'me' ;
1239
+ element = $compile ( '<div><b cau test="2"></b><b cau test="3"></b></div>' ) ( $rootScope ) ;
1240
+
1241
+ $rootScope . $digest ( ) ;
1242
+
1243
+ expect ( sortedHtml ( element ) ) .
1244
+ toEqual ( '<div><b test="2"><span>Hey, me!</span></b><b test="3">' +
1245
+ '<span>Say: Hey, me!</span></b></div>' ) ;
1246
+ }
1247
+ ) ) ;
1248
+ } ) ;
1249
+
1158
1250
1159
1251
describe ( 'scope' , function ( ) {
1160
1252
var iscope ;
0 commit comments