Directives and scope

Description

A-Level Angular Note on Directives and scope, created by James Drummond on 03/05/2015.
James Drummond
Note by James Drummond, updated more than 1 year ago
James Drummond
Created by James Drummond almost 9 years ago
10
1

Resource summary

Page 1

An Angular directive inherits its parent's scope by default.Modify the scope of the directive using the property 'scope' in the directive object.scope : false- This is the default. The directive does not create a new scope, it just uses the parent one.- There is no inheritance but this is not generally a good way of creating re-usable componentsscope : true- Directive has a new scope which prototypically inherits from the parent scope- If more than one directive (under the same parent) requests a new scope, only one scope will be created. (As you would expect: all models should be in sync across duplicate directives)- Be wary of two way data-binding to primitives in the parent scope (do not inherit from primitives in the parent scope as data binding will not work).

scope : {}- Creates new scope, nothing inherited from parentInheriting distinct items from parent$scope.car = {type : 'hyundai', doors : 3}// Cannot insert html due to idiocy from GoConqryou need to create an attribute on the element: carProperty=car // Note no quotes around car. car can be an object.scope : {localProperty : '=car-property'} // Two directional binding with car objectattribute on element : car-name="{{car.name}}" scope : { carName : '@carName' } // Only inherits a string!! Not two way binding!attribute on element:sayhelloFunction='sayHelloThere()'; // sayHello() is function in the parent scopescope : { sayHello : '&sayHelloFunction'} // Now if you call sayHello in the directive, sayHelloThere will be called in the parent

Show full summary Hide full summary

Similar

Directives, compile and link
James Drummond
The Princeton Review Chapter 9 Rotational Motion
Anibal Santamaria
Angular
Kingsley Bawuah
JavaScript Fundamentals
Andrew Watters
Direct Effect - European Union
Tatiana Nikolaid
Scope and Inheritance in Angular
James Drummond
Angular Nuts and Bolts
James Drummond
Direct Effect - EU Public Law
Tatiana Nikolaid
Angular application scaffolding
James Drummond
Direct Effect and Union Acts
cadhla_corrigan
FE Web Applications Components
Juan Carlos Joya Carvajal