@@ -585,6 +585,49 @@ describe('$compile', function() {
585
585
} ) ;
586
586
587
587
588
+ describe ( 'template function' , function ( ) {
589
+
590
+ beforeEach ( module ( function ( ) {
591
+ directive ( 'replace' , valueFn ( {
592
+ restrict : 'CAM' ,
593
+ replace : true ,
594
+ template : function ( f ) {
595
+ return '<div class="log" style="width: 10px" high-log>Replace!</div>'
596
+ } ,
597
+ compile : function ( element , attr ) {
598
+ attr . $set ( 'compiled' , 'COMPILED' ) ;
599
+ expect ( element ) . toBe ( attr . $$element ) ;
600
+ }
601
+ } ) ) ;
602
+
603
+ directive ( 'replaceattr' , valueFn ( {
604
+ restrict : 'CAM' ,
605
+ replace : true ,
606
+ template : function ( f ) {
607
+ return '<div class="log" style="width: 10px" high-log ' + f . myattr + '="123">Replace!</div>'
608
+ } ,
609
+ compile : function ( element , attr ) {
610
+ expect ( attr . dynamic ) . toBe ( '123' ) ;
611
+ attr . $set ( 'dynamic' , '456' ) ;
612
+ }
613
+ } ) ) ;
614
+ } ) ) ;
615
+
616
+
617
+ it ( 'should replace element with template returned by function' , inject ( function ( $compile , $rootScope ) {
618
+ element = $compile ( '<div><div replace>ignore</div><div>' ) ( $rootScope ) ;
619
+ expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
620
+ expect ( element . find ( 'div' ) . attr ( 'compiled' ) ) . toEqual ( 'COMPILED' ) ;
621
+ } ) ) ;
622
+
623
+ it ( 'should pass attributes to template function' , inject ( function ( $compile , $rootScope ) {
624
+ element = $compile ( '<div><div replaceattr myattr="dynamic">ignore</div><div>' ) ( $rootScope ) ;
625
+ expect ( element . text ( ) ) . toEqual ( 'Replace!' ) ;
626
+ expect ( element . find ( 'div' ) . attr ( 'dynamic' ) ) . toEqual ( '456' ) ;
627
+ } ) ) ;
628
+ } ) ;
629
+
630
+
588
631
describe ( 'templateUrl' , function ( ) {
589
632
590
633
beforeEach ( module (
@@ -1115,6 +1158,53 @@ describe('$compile', function() {
1115
1158
} ) ;
1116
1159
} ) ;
1117
1160
1161
+ describe ( 'templateUrl function' , function ( ) {
1162
+
1163
+ beforeEach ( module (
1164
+ function ( ) {
1165
+ directive ( 'hello' , valueFn ( {
1166
+ restrict : 'CAM' , templateUrl : function ( t ) {
1167
+ return 'hello.html' ;
1168
+ } ,
1169
+ transclude : true
1170
+ } ) ) ;
1171
+ directive ( 'cau' , valueFn ( {
1172
+ restrict : 'CAM' , templateUrl : function ( t ) {
1173
+ return 'cau' + t . test + '.html' ;
1174
+ }
1175
+ } ) ) ;
1176
+ }
1177
+ ) ) ;
1178
+
1179
+ it ( 'should compile, link and flush the template inline when using functions as templateUrl' , inject (
1180
+ function ( $compile , $templateCache , $rootScope ) {
1181
+ $templateCache . put ( 'hello.html' , '<span>Hello, {{name}}!</span>' ) ;
1182
+ $rootScope . name = 'Elvis' ;
1183
+ element = $compile ( '<div><b hello></b></div>' ) ( $rootScope ) ;
1184
+
1185
+ $rootScope . $digest ( ) ;
1186
+
1187
+ expect ( sortedHtml ( element ) ) .
1188
+ toEqual ( '<div><b><span>Hello, Elvis!</span></b></div>' ) ;
1189
+ }
1190
+ ) ) ;
1191
+
1192
+ it ( 'should pass attributes to the templateUrl function' , inject (
1193
+ function ( $compile , $templateCache , $rootScope ) {
1194
+ $templateCache . put ( 'cau2.html' , '<span>Hey, {{name}}!</span>' ) ;
1195
+ $templateCache . put ( 'cau3.html' , '<span>Say: Hey, {{name}}!</span>' ) ;
1196
+ $rootScope . name = 'me' ;
1197
+ element = $compile ( '<div><b cau test="2"></b><b cau test="3"></b></div>' ) ( $rootScope ) ;
1198
+
1199
+ $rootScope . $digest ( ) ;
1200
+
1201
+ expect ( sortedHtml ( element ) ) .
1202
+ toEqual ( '<div><b test="2"><span>Hey, me!</span></b><b test="3">' +
1203
+ '<span>Say: Hey, me!</span></b></div>' ) ;
1204
+ }
1205
+ ) ) ;
1206
+ } ) ;
1207
+
1118
1208
1119
1209
describe ( 'scope' , function ( ) {
1120
1210
var iscope ;
0 commit comments