Professional by Valeri Karpov & Diego Netto
Author:Valeri Karpov & Diego Netto
Language: eng
Format: epub
ISBN: 9781118832080
Published: 2015-04-16T00:00:00+00:00
The Second Way of Using the scope Setting
Recall that the second way of using the scope setting, specifying a JavaScript object (possibly empty, that is, { }), creates a new scope for the directive that does not have a parent scope. Thus, the scope.$parent.$watch() pattern from the previous section does not work, because the directive’s scope is outside the page’s scope hierarchy. If you’re worried that this means your directives will not have a way to access variables in the page’s scope, don’t worry—AngularJS provides a slick way to pull outside variables into the isolate scope. Here’s an example of how this works:
module.directive('imageCarousel', function() { return { template: '<div my-background-image="images[currentIndex]"' + ' ng-swipe-left="next()"' + ' ng-swipe-right="previous()"' + ' style="height: 120px; width: 600px; border: 1px solid red">' + '</div>' + '<input type="button" toggle-button="disabled">' + '<h1>Image index: {{currentIndex}}</h1>', controller : CarouselController, scope : { images : '=imageCarousel' } } });
The =imageCarousel syntax in the scope setting is a handy shortcut for the scope.$parent.$watch() call you did in the previous section. In the scope setting, = tells AngularJS that the images variable should be bound to the variable specified in the imageCarousel attribute, with the understanding that the imageCarousel attribute should be evaluated in the directive’s parent scope. You will see this shortcut used extensively in AngularJS directive code, so make sure you remember its semantics. In particular, make sure you remember that, in the scope setting, the object keys are the variables in the scope, and the object values refer to the HTML attribute that the scope variable should be bound to.
But doesn’t this shortcut defeat the point of having an isolate scope? Actually, if you want a strict isolate scope, where there is no data binding between the isolate scope and the page’s scope structure, you can use an empty object { } for the scope setting. However, the use cases for strict isolate scopes are somewhat limited, because such a directive must be entirely self-contained. Such directives are often called components and will be discussed later in this chapter.
The = shortcut for isolate scopes serves two primary functions. First, writing =imageCarousel is far more concise than writing out the full scope.$parent.$watch() call. Second, the fact that the scope is marked as isolate ensures that the directive’s template doesn’t access any variables outside the directive except for as explicitly specified in the scope setting. This makes your directives easier to understand and use, because your client is guaranteed that your directive only interacts with the outside world through the scope setting. The isolate scope also serves as a preventive measure against silly mistakes. For example, try to spot the bug in the following code:
module.directive('imageCarousel', function() { return { template: '<div my-background-image="defaultImages[currentIndex]"' + ' ng-swipe-left="next()"' + ' ng-swipe-right="previous()"' + ' style="height: 120px; width: 600px; border: 1px solid red">' + '</div>' + '<input type="button" toggle-button="disabled">' + '<h1>Image index: {{currentIndex}}</h1>', controller : CarouselController, scope : true, link : function(scope, element, attributes) { scope.$parent.$watch(attributes.imageCarousel, function(v) { scope.images = v; }); } } });
The myBackgroundImage directive in the template uses the defaultImages variable, which is defined in the parent scope.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9926)
The Mikado Method by Ola Ellnestam Daniel Brolund(9786)
Dependency Injection in .NET by Mark Seemann(9347)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7787)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7566)
Svelte with Test-Driven Development by Daniel Irvine(7228)
Test-Driven Development with PHP 8 by Rainier Sarabia(6963)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6829)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6539)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6423)
Web Development with Django by Ben Shaw Saurabh Badhwar(6292)
React Application Architecture for Production by Alan Alickovic(6010)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5813)
Kotlin in Action by Dmitry Jemerov(5073)
Audition by Ryu Murakami(4590)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4488)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4335)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4323)
Solidity Programming Essentials by Ritesh Modi(4045)
