Angular

$scope behavior in the directives

What is Directive?

In Any application if we want some specific functionality which we need to reuse in application, then for this we need to develop set of codes it calls Directive.

I am not going in detail of directive, I assume that you have sufficient knowledge of directive so, I am just focus on use of $scope into directive

Scopes in AngularJS

$scope has a very important role in AngularJS, it works as a mediator between the Controller & View in an Angular. Each scope object created by controller or any other directive or service are inherited from rootscope.

$scope into directive

All directive have a scope associated with it. Directive use scope for accessing data & Methods inside the template and link function. Directive never create its own scope unless explicitly set. By default directive use their parent scope (from where it is called).

We can change the default scope of the directive using scope property just like restrict, template, etc. the value of scope property define how scope of directive is behave. We can define scope property by 3 ways, following are different scenario of using scope inside directive:

  • The scope is not assigned or set as false
  • The scope set as true
  • Isolated scope
Scope: false (Directive uses its parent scope)

This is default value of scope in directive. When we declare scope: false in DDO (directive definition object) at that time the directive has the same scope as its parent controller.

in above example if we change name in textbox it also change header value because of directive use parent controller scope so any changes inside directive are reflected in the controller scope (Parent scope) same as any change in controller also reflected in directive.

Scope: true (Directive gets a new scope)

When we declare scope: true in DDO at that time directive define its own scope. This new scope inherited from its parent controller scope. In this case scope binding flow is one direction only from parent to directive scope so any change in directive not reflected in parent scope but when we change parent scope it reflect in directive scope

In above example if we change name in header textbox then it reflect in directive scope but when we change in directive scope it not reflect in parent scope, if you want to reflect parent scope then you need to use $parent for that

Scope: { } (Directive gets the isolated scope)

When we declare scope: {} as a object in DDO then directive will create a new isolated scope. and scope is not inherited from parent scope. so if we want to access parent scope values then we need to declare as a scope property in DDO and that property assignee through directive attributes.

In isolated scope we can define scope property by using three prefixes. These are @ : One way binding =  : Two way binding , & : Method binding

In above example if we change name in parent scope so its reflect in directive scope but if we change name in directive scope then it’s not reflect in parent scope due to one way binding, same as if we change last name in parent scope it reflect in directive scope and also if we change last name in directive scope then it reflect in parent scope due to two way binding. Same as we can also pass method of parent scope in directive scope and call from directive.

About Triveni

Triveni is such strong and trustworthy name in the field of customised IT solution provider which is earnest name in Indian IT sector.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Settings
We use cookies to enhance your experience while using our website. If you are using our Services via a browser you can restrict, block or remove cookies through your web browser settings. We also use content and scripts from third parties that may use tracking technologies. You can selectively provide your consent below to allow such third party embeds. For complete information about the cookies we use, data we collect and how we process them, please check our Privacy Policy
Youtube
Consent to display content from Youtube
Vimeo
Consent to display content from Vimeo
Google Maps
Consent to display content from Google
Spotify
Consent to display content from Spotify
Sound Cloud
Consent to display content from Sound