What Is Isolated Scope in AngularJs ?

13-Sep-2022

If you are familiar with Angular8, you may know or have heard about Isolated scope in AngularJs. While the scope feature is an inbuilt module that by default originated from a parent scope, Isolated Scope does not inherit from a parent scope and exists on its own. We will learn in detail about Isolated scope in AngularJS here in this post. 

 

To understand Isolated Scope, let us first have a brief look into the features of scope. The object scope is a reference to the application model. It serves as an expression execution context. Scopes are organized in a hierarchical pattern that reflects the application's DOM structure. Scopes can observe expressions and spread events.

 

Scopes offer APIs ($watch) for tracking model changes.

Scopes offer APIs ($apply) to propagate any model changes made outside the "AngularJS realm" across the system into the view (controllers, services, AngularJS event handlers).

To restrict access to application component attributes while allowing access to shared model properties, scopes can be nested. Child scopes or isolated scopes are both types of nested scopes. (Prototypically) A "child scope" inherits attributes from its parent scope. It does not occur with an "isolate scope." Isolated scopes can provide extra details.

Defining Isolated Scope in AngularJs:

What is isolated Scope in AngularJs?

To put it in simple terms, the Isolated scope in AngularJS is one that hasn't been conventionally created out of a parent scope. Still, it does have the ability to access the parent scope via $parent. The isolated scope directive in Python has the following three fundamental characteristics.

 

  • scope: false: This directive is typically used to reuse the scope from the component it is presently being used at and is used by default in isolated scope.

  • scope: true: When a child scope is created using this directive, it normally inherits the parent scope.

  • scope:{...}: This is a prototype representation of the parent scope and is used to build the isolated scope.

 

To have a clearer understanding of the above directives, let us consider the below explanation:

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. 

 

data bind (=)

interpolate (@)

expression (&)

Example

scope:     

{     

   myValue1 : '@attribute1',    

   myValue2: '=attribute2',    

   myValue3: '&attribute3'      

}  

Adapt or Attributes (@)

This is used to link the DOM attribute to the isolated scope. With this, a one-way directive is essentially set up between the isolated scope and the parent scope. This implies that any modifications made to the Parent Scope will be immediately reflected in the isolated scope as well.

Example-

.directive('myDirective', function () {  

    return {  

        scope:{  

            myAttribute:'@',  

        }          

    };  

})

 

Expression (&)

For calling a function from the parent scope to the isolated scope, the Expression (&) is applied. It is mainly used, among other things, to produce callbacks.

Example-

.directive('myDirective', function () { 

    return { 

        scope:{ 

            myIsolatedFunction:'&' 

        }         

    }; 

}) 

 

We will apply 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- The main distinction between Binding (=) and Attributes is that Binding establishes a two-way channel of communication rather than just one.

Example 

.directive('myDirective', function () { 

    return { 

        scope:{ 

            myBinding:'=', 

        }         

    }; 

})  

 

(=) Binding



Angular 8's Foreach function

Now that you are familiar with the fundamentals of Isolated scope in AngularJS, let's investigate the foreach method as well.

 

In Angular8, the following methods are employed to enable Foreach:

Step 1: Launch Visual Editor 2012, then select File > New > Project. Name the file forOrforeach when the window first appears, and then click the OK button.

 

Step #2: Open the Solution Explorer, which contains the html file, css file, js file, and ts file, after completing step 1.

 

Step #3: Run the foreach function code that follows now.

 

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(); 

}  

 

An essential component of an AngularJS directive is the isolated scope in AngularJs. By using the isolated scope in angularjs, it is prevented from being contaminated by scope outside of the directive component. It is really helpful and significant since we don't want our actions inside the control or template to have an impact on or be affected by properties on the Parent scope.

 

Post a Comment

Submit
Top