@@ -1216,6 +1216,182 @@ describe('$compile', function() {
1216
1216
} ) ;
1217
1217
} ) ;
1218
1218
1219
+ it ( 'allows binding expression to isolate scope' , function ( ) {
1220
+ var givenScope ;
1221
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1222
+ return {
1223
+ scope : {
1224
+ anAttr : '='
1225
+ } ,
1226
+ link : function ( scope ) {
1227
+ givenScope = scope ;
1228
+ }
1229
+ } ;
1230
+ } ) ;
1231
+ injector . invoke ( function ( $compile , $rootScope ) {
1232
+ var el = $ ( '<div my-directive an-attr="42"></div>' ) ;
1233
+ $compile ( el ) ( $rootScope ) ;
1234
+
1235
+ expect ( givenScope . anAttr ) . toBe ( 42 ) ;
1236
+ } ) ;
1237
+ } ) ;
1238
+
1239
+ it ( 'allows aliasing expression attribute on isolate scope' , function ( ) {
1240
+ var givenScope ;
1241
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1242
+ return {
1243
+ scope : {
1244
+ myAttr : '=theAttr'
1245
+ } ,
1246
+ link : function ( scope ) {
1247
+ givenScope = scope ;
1248
+ }
1249
+ } ;
1250
+ } ) ;
1251
+ injector . invoke ( function ( $compile , $rootScope ) {
1252
+ var el = $ ( '<div my-directive the-attr="42"></div>' ) ;
1253
+ $compile ( el ) ( $rootScope ) ;
1254
+
1255
+ expect ( givenScope . myAttr ) . toBe ( 42 ) ;
1256
+ } ) ;
1257
+ } ) ;
1258
+
1259
+ it ( 'evaluates isolate scope expression on parent scope' , function ( ) {
1260
+ var givenScope ;
1261
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1262
+ return {
1263
+ scope : {
1264
+ myAttr : '='
1265
+ } ,
1266
+ link : function ( scope ) {
1267
+ givenScope = scope ;
1268
+ }
1269
+ } ;
1270
+ } ) ;
1271
+ injector . invoke ( function ( $compile , $rootScope ) {
1272
+ $rootScope . parentAttr = 41 ;
1273
+ var el = $ ( '<div my-directive my-attr="parentAttr + 1"></div>' ) ;
1274
+ $compile ( el ) ( $rootScope ) ;
1275
+
1276
+ expect ( givenScope . myAttr ) . toBe ( 42 ) ;
1277
+ } ) ;
1278
+ } ) ;
1279
+
1280
+ it ( 'watches isolated scope expressions' , function ( ) {
1281
+ var givenScope ;
1282
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1283
+ return {
1284
+ scope : {
1285
+ myAttr : '='
1286
+ } ,
1287
+ link : function ( scope ) {
1288
+ givenScope = scope ;
1289
+ }
1290
+ } ;
1291
+ } ) ;
1292
+ injector . invoke ( function ( $compile , $rootScope ) {
1293
+ var el = $ ( '<div my-directive my-attr="parentAttr + 1"></div>' ) ;
1294
+ $compile ( el ) ( $rootScope ) ;
1295
+
1296
+ $rootScope . parentAttr = 41 ;
1297
+ $rootScope . $digest ( ) ;
1298
+ expect ( givenScope . myAttr ) . toBe ( 42 ) ;
1299
+ } ) ;
1300
+ } ) ;
1301
+
1302
+ it ( 'allows assigning to isolated scope expressions' , function ( ) {
1303
+ var givenScope ;
1304
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1305
+ return {
1306
+ scope : {
1307
+ myAttr : '='
1308
+ } ,
1309
+ link : function ( scope ) {
1310
+ givenScope = scope ;
1311
+ }
1312
+ } ;
1313
+ } ) ;
1314
+ injector . invoke ( function ( $compile , $rootScope ) {
1315
+ var el = $ ( '<div my-directive my-attr="parentAttr"></div>' ) ;
1316
+ $compile ( el ) ( $rootScope ) ;
1317
+
1318
+ givenScope . myAttr = 42 ;
1319
+ $rootScope . $digest ( ) ;
1320
+ expect ( $rootScope . parentAttr ) . toBe ( 42 ) ;
1321
+ } ) ;
1322
+ } ) ;
1323
+
1324
+ it ( 'gives parent change precedence when both parent and child change' , function ( ) {
1325
+ var givenScope ;
1326
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1327
+ return {
1328
+ scope : {
1329
+ myAttr : '='
1330
+ } ,
1331
+ link : function ( scope ) {
1332
+ givenScope = scope ;
1333
+ }
1334
+ } ;
1335
+ } ) ;
1336
+ injector . invoke ( function ( $compile , $rootScope ) {
1337
+ var el = $ ( '<div my-directive my-attr="parentAttr"></div>' ) ;
1338
+ $compile ( el ) ( $rootScope ) ;
1339
+
1340
+ $rootScope . parentAttr = 42 ;
1341
+ givenScope . myAttr = 43 ;
1342
+ $rootScope . $digest ( ) ;
1343
+ expect ( $rootScope . parentAttr ) . toBe ( 42 ) ;
1344
+ expect ( givenScope . myAttr ) . toBe ( 42 ) ;
1345
+ } ) ;
1346
+ } ) ;
1347
+
1348
+ it ( 'throws when binding array-returning function to isolate scope' , function ( ) {
1349
+ var givenScope ;
1350
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1351
+ return {
1352
+ scope : {
1353
+ myAttr : '='
1354
+ } ,
1355
+ link : function ( scope ) {
1356
+ givenScope = scope ;
1357
+ }
1358
+ } ;
1359
+ } ) ;
1360
+ injector . invoke ( function ( $compile , $rootScope ) {
1361
+ $rootScope . parentFunction = function ( ) {
1362
+ return [ 1 , 2 , 3 ] ;
1363
+ } ;
1364
+ var el = $ ( '<div my-directive my-attr="parentFunction()"></div>' ) ;
1365
+ $compile ( el ) ( $rootScope ) ;
1366
+ expect ( function ( ) {
1367
+ $rootScope . $digest ( ) ;
1368
+ } ) . toThrow ( ) ;
1369
+ } ) ;
1370
+ } ) ;
1371
+
1372
+ it ( 'can watch isolated scope expressions as collections' , function ( ) {
1373
+ var givenScope ;
1374
+ var injector = makeInjectorWithDirectives ( 'myDirective' , function ( ) {
1375
+ return {
1376
+ scope : {
1377
+ myAttr : '=*'
1378
+ } ,
1379
+ link : function ( scope ) {
1380
+ givenScope = scope ;
1381
+ }
1382
+ } ;
1383
+ } ) ;
1384
+ injector . invoke ( function ( $compile , $rootScope ) {
1385
+ $rootScope . parentFunction = function ( ) {
1386
+ return [ 1 , 2 , 3 ] ;
1387
+ } ;
1388
+ var el = $ ( '<div my-directive my-attr="parentFunction()"></div>' ) ;
1389
+ $compile ( el ) ( $rootScope ) ;
1390
+ $rootScope . $digest ( ) ;
1391
+ expect ( givenScope . myAttr ) . toEqual ( [ 1 , 2 , 3 ] ) ;
1392
+ } ) ;
1393
+ } ) ;
1394
+
1219
1395
} ) ;
1220
1396
1221
1397
} ) ;
0 commit comments