1

I am trying to learn the difference between JavaScript Scope and Angularjs scope. When I searched online and in SO i found Scope in JavaScript has two scopes: global and local they are mentioning for variable But in Angular $scope is the application object I am not understanding clearly can somebody help me with some example what is scope..... I want know whether scope only the word is same meaning for both the technologies are different kindly explain me

Thanks

5
  • 2
    They are entirely different things, but work in a related way. I'm not sure how productive it is to compare the two without confusing the issue further. You need to learn three things: how Javascript scope works, period; how the $scope object works in Angular; how Angular templates interact with the $scope object. Commented Mar 15, 2016 at 13:00
  • i understand from the online scope in javascript is something variable is that correct or my understanding was wrong Commented Mar 15, 2016 at 13:02
  • This is extremely well documented in angular docs Commented Mar 15, 2016 at 13:40
  • what is wrong in question kindly please explain me why i am getting negative mark Commented Mar 15, 2016 at 13:40
  • 1
    See AngularJS - What are Scopes? Commented Mar 15, 2016 at 13:42

1 Answer 1

4

JavaScript

Scope in JavaScript is the variables to which a particular piece of code has access. It includes the variables in the current function, the variables in any containing functions, and all global variables.

For example here is a simple JavaScript that shows some variables that are and are not in scope for a particular piece of code:

<script type="text/javascript">
    var a = 1; // in scope

    function W() {
        var b = 2; // not in scope
    }

    function X() {
        var c = 3; // in scope

        function Y() {
            var d = 4; // not in scope
        }

        function Z() {
            var e = 5; // in scope

            // here is my code
            // it has access to a, c, and e
            a = c + e;
        }

    }
</script>

The code in function Z has access to variables a, c, and e, but not b or d.

AngularJS

AngularJS implements a variation of the MVC Pattern. The $scope variable is the "model" part of the pattern. The properties that you assign to $scope become the variables that are visible to the HTML that uses the scope.

For example, consider this simple example:

app.controller('MyController', function ($scope) {
    $scope.message = 'Hello Cleveland!';
}

.

<div ng-controller="MyController">
    <span>{{message}}</span>
</div>

You assign $scope.message in the controller and it is visible as message in the HTML.

Scopes in Angular also behave similar to scopes in JavaScript because they inherit from their parents. In Angular, there is the $rootScope that all $scopes in the application inherit from. Each controller creates a new scope that is a child of the root scope, and child scopes are often created by directives. For example, the ng-if directive creates a child scope.

app.controller('MyController', function ($scope) {
    $scope.message = 'Hello Cleveland!';
    $scope.show = true;
}

.

<div ng-controller="MyController">
    <div ng-if="show">
        <span>{{message}}</span>
    </div>
</div>

In this example, the span is actually in a child scope created by ng-if, but it still has access to the message property because it inherited it from its parent.

Likewise, you can have sibling scopes that don't interact.

For example, say you have two controllers, "Ctrl1" and "Ctrl2":

app.controller('Ctrl1', function ($scope) {
    $scope.message = 'This controller is #1';
}
app.controller('Ctrl2', function ($scope) {
    $scope.message = 'This is the second controller';
}

.

<div ng-controller="Ctrl1">
    <span>{{message}}</span>
</div>
<div ng-controller="Ctrl2">
    <span>{{message}}</span>
</div>

The message variable is distinct in each one because there is no inheritance relationship between the two.

Difference

There is one significant difference between the behavior of scope in JavaScript and in Angular. This behavior is due to the fact that scopes in Angular use prototypical inheritance. There is a lot that has been written about this already, so I won't go into too much detail, but the important thing to remember is this:

Changing a variable in a child scope does not affect the same-named variable in the parent scope.

Each child scope effectively gets its own copy of all of the inherited properties. When the child scope modifies it, it only modifies its copy, not the parent's.

This is not the case in JavaScript. In the first example, the Z function assigns a value to the global a variable. This change will be visible to all of the code. The same would not be true in Angular.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.