Introduction
While we create a custom directive, there is a possibility that you will want to use the scopes available in the parent controller. You can use the scopes of the parent controller in different levels. This article will help you choose in between those levels.
I created a sample application which will help you understand it in an easy manner.
Step 1
Firstly, I added a HTML page where our custom control is created.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <script src="angular.js"></script>
- <script src="AngularScript.js"></script>
- </head>
- <body ng-app="angularApp">
- <form ng-controller="angularController">
- <fieldset>
- <legend>I am binding from controller
- </legend>
- <input type="text" ng-model="EnterName" />
- </fieldset>
-
- <br />
- <fieldset>
- <legend>I am binding from directive
- </legend>
- <custom-directive></custom-directive>
- </fieldset>
- </form>
-
- </body>
- </html>
In this code you can see I had bound the form tag with a controller named "angularController," this controller will be created in upcoming steps.
In this form I used two fieldsets to distinguish between normal controller binding and custom directive. In the first fieldset I created an input control which is bound to a model named "EnterName."
In the second fieldset I created a custom control named "custom-directive," using this control I'll create the custom directive.
Step 2
Now it's time to work on the angular part. I added a JavaScript file to my application in which I had written my angular code.
Firstly, I created a module and a controller.
- var angularApp = angular.module('angularApp', []);
- angularApp.controller('angularController', function ($scope) {
- $scope.EnterName = "Anubhav";
- });
Now it's time to create our custom directive, but before creating it, let's first know how scopes will be used in this directive or what level will be used here.
scope : false
So, in this directive you will be able to access the scopes completely without having any kind of restriction. Let's first see the code and after that I'll explain it.
- angularApp.directive("customDirective", function () {
- return {
- restrict: "E",
- scope: false,
- template: "<div>Name you Entered is : {{EnterName}}</div>" +
- "Provide Your Name : <input type='text' ng-model='EnterName' />"
- };
- });
You can see that I wrote "scope : false," it means custom directive will not have its own new scope and it'll inherit its parent scope completely. So whatever model you have defined in your controller can be easily used here, just as I have used in the template. One more important thing with "scope : false" is that if you make changes in scope data then that will be reflected in both controller and in directive as well.
Let's see the output to clearly understand it.
You can see that both the textboxes and label have the predefined data, now if I change the data in any of the textbox then changes will occur in all the controls which are bound to this model.
scope : true
It might be possible that you want to use the value of parent scopes for one time only i.e. at initial stage but after that you don't want your directive scope to get updated if there is a change at parent scope or vice versa, then in that case just provide "scope : true."
- angularApp.directive("customDirective", function () {
- return {
- restrict: "E",
- scope: true,
- template: "<div>Name you Entered is : {{EnterName}}</div>" +
- "Provide Your Name : <input type='text' ng-model='EnterName' />"
- };
- });
Now let's again run the application and check what is happening.
As the page loads all the controls get their initial value which was predefined in the controller, but if I change the data in second textbox i.e. at custom directive level, then only those controls will get updated which are created using custom directive and everyone else will remain the same.
scope : { }
Scope :true was providing you a new scope but after the initial level, what if you do not want any kind of interference between controller and directive i.e. you do not want to use any value from controller? Therefore, in that case make isolated scope i.e. "scope : {}"
- var angularApp = angular.module('angularApp', []);
- angularApp.controller('angularController', function ($scope) {
- $scope.EnterName = "Anubhav";
- });
-
- angularApp.directive("customDirective", function () {
- return {
- restrict: "E",
- scope: {},
- template: "<div>Name you Entered is : {{EnterName}}</div>" +
- "Provide Your Name : <input type='text' ng-model='EnterName' />"
- };
- });
You can see that still in the template I bound the controls with the same model to which they were earlier bound but now this model will not have any impact from previous one because they are defined in a new scenario.
Let's again run the application and check what's happening.
You can see that only first textbox is getting the value but our label and second textbox is empty. But if I try to write any value in the second textbox then label also get updated because it's bound to the same model to which second textbox is bound.
Isolated scopes is a new topic in itself and I'll explain it in my next article.