AngularJS (39 Blogs) Become a Certified Professional

What is Isolated Scope in Angular8?

Published on Nov 18,2019 92 Views
7 / 11 Blog from Introduction to Angular

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

If you have been using Angular8 for some time now, you have probably heard of the scope feature. It is an inbuilt module in Angular8 and has originated from the Parent Scope default. But most of the time, it is not required in the development of common components and that’s how isolated scope came into the picture. In this article, we will discuss all about the isolated scope in Angular8 in the following order:

Let’s begin.

What is isolated scope in Angular8?

Isolated scope in Angular8 has not been prototypically derived from parent scope, but it does the feature to access the parent scope through $parent. The isolated scope directive in Python has three basic properties, which are as listed below. 

  1. scope: false: This directive is used by default in isolated scope and it is mainly used to reuse the scope from the component it is currently being used at. 

  2. scope: true: This directive is used to create a child scope and it prototypically inherits the parent scope. 

  3. scope: {…}: This is used to create the isolated scope and this is prototypically from the parent scope. 

To understand the above directives better, take a look at the explanations below. 

scope: true 

Parent Scope <<======prototype======  Child Scope

                         <<====== $Parent ======   Child Scope

scope: {}

Parent Scope <<=====$Parent Scope ========= Isolates Scope 

Here we need to make use of three types of interfaces between the isolated scope and the parent scope. 

  1. data bind (=)
  2. interpolate (@)

  3. expression (&) 

Examples

scope:     
{     
   myValue1 : '@attribute1',    
   myValue2:   '=attribute2',    
   myValue3:    '&attribute3'    
  
}   

Interpolate or Attributes (@)

This is used to bind the isolated scope with the DOM attribute. This basically setups a one way directive to be used between the isolated scope and the parent scope. What this means is that if there are any changes in the Parent Scope, the same will instantaneously be reflected in the isolated scope as well.

Example

.directive('myDirective', function () {  
    return {  
        scope:{  
            myAttribute:'@',  
        }          
    };  
})  

Now we can make use of a directive to bind these two toghether. 

<my-directive my-attribute="{{Hello Geeks}}"></my-directive>  

Expression (&)

The Expression (&) is used to call a function from the parent scope to the isolated scope. It is mainly used to create call backs among other functions. 

Example

.directive('myDirective', function () {  
    return {  
        scope:{  
            myIsolatedFunction:'&'  
        }          
    };  
})  

Now we will use DOM

<input ng-model="myIsolated">  
<button class="myButton" ng-click="myIsolatedFunction({myValue:myIsolated})">Click OK</button>  

The method will now call back to the controller in the following manner. 

.controller('myTestController', ['$scope', function ($scope) {  
    $scope.myUpdatedValue= function (myValue) {  
        $scope.updatedValue= myValue;  
    }  
}]);  

Binding (=)

Binding (=) is similar to Attributes, the only difference being that it creates a two way communication channel instead of one. 

Example 

.directive('myDirective', function () {  
    return {  
        scope:{  
            myBinding:'=',  
        }          
    };  
})   

Foreach function in Angular8

Now that you know the basic concept of the isolated scope in Angular8, let us explore the foreach function as well.

Foreach in Angular8

To enable the foreach function in Angular8, follow the steps outlined below. 

Step #1

Start Visual Editor 2012 and navigate to Project under New and File. When the window opens name the file as forOrforeach and then click the OK button. 

Step #2

Once step 1 is completed, open the Solution Explorer which contains css file, js file, ts file as well as html file. 

Step #3

Now run the following code for the foreach function. 

forOrforeach.ts

class A {  
    function() {  
        var array = [1, 2, 3, 4];  
        for (var v in array) // for acts as a foreach  
        {  
            alert(array[v]);  
        }  
    }  
}  
window.onload = () => {  
    var call = new A();  
    call.function();  
}   

Default.html

<!DOCTYPEhtml>  
<htmllang="en" xmlns="http://www.w3.org/1999/xhtml">  
  
    <head>  
        <metacharset="utf-8" />  
        <title>TypeScript HTML App</title>  
        <linkrel="stylesheet" href="app.css" type="text/css" />  
        <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscriptsrc%3D%22app.js%22%3E%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />  
    </head>  
  
    <body>  
       
<h1>For loop as foreach loop in TypeScript</h1>

        <divid="content" /> </body>  
  
    </html> 

App.js

var A = (function() {  
    function A() {}  
    A.prototype.function = function() {  
        var array = [  
            1,  
            2,  
            3,  
            4  
        ];  
        for (var v in array) {  
            alert(array[v]);  
        }  
    };  
    return A;  
})();  
window.onload = function() {  
    var call = new A();  
    call.function();  
};   

The output for the above codes will look something like this.

Output - isolated scope in angular8 - Edureka

forEach in TypeScript

Now let us explore how to implement a foreach statement in TypeScript. 

Newforeach.ts

class A {  
    no: number[] = [1, 2, 3];  
    lognumber() {  
        this.no.forEach((nos) => { // foreach statement  
            document.write(" number=:" + nos);  
        })  
    }  
}  
window.onload = () => {  
    var call = new A();  
    call.lognumber();  
}  

The output of the above will be,

Output - isolated scope in angular8 - Edureka

Now that you know how to use both the foreach as well as the isolated scope modules, we hope that you will make use of the same in your day to day Angular8 coding to make it more efficient. Learn the basics as well as the uses of both foreach as well as isolated scope in Angular8. 

This brings us to the end of Angular Tutorial blog. I hope this blog was informative and added value to you. Now, you must be clear with the building blocks of Angular and ready to create an Angular application. I would recommend you to go through this Angular Tutorial Edureka video playlist to watch videos and learn how to create an Angular application.

Check out the Angular training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework which is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community driven indirectly driving better job opportunities. The Angular Certification Training aims at covering all these new concepts around Enterprise Application Development.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.