Runoob Tutorial ArchiveEnglish

AngularJS Scope

AngularJS Scope


Scope is the link between HTML (view) and JavaScript (controller).

Scope is an object with available methods and properties.

Scope can be applied to views and controllers.


How to use Scope

When you create a controller in AngularJS, you can pass$scopePassing an object as a parameter:

AngularJS instance

Properties in the controller correspond to properties in the view:

<

div

ng-app=

"myApp"

ng-controller=

"myCtrl"

<

h1

{{carname}}

<

/h1

<

/div

<

script

var

app = angular.module(

'myApp'

, []);

app.controller(

'myCtrl'

,

function

($scope) {

    $scope.carname =

"Volvo"

;

});

<

/script

Try it »

When adding$scopeobject in the controller, the view (HTML) can access these properties.

In the view, you do not need to add$scopeprefix, just add the property name, such as:{{carname}}.


Scope Overview

An AngularJS application is composed of the following:

scope is the model.

scope is a JavaScript object that has properties and methods that can be used in the view and controller.

AngularJS instance

If you modify the view, the model and controller will also be updated accordingly:

<div ng-app="myApp" ng-controller="myCtrl">
<input ng-model="name">
<h1{{greeting}}>
</h1>
<button ng-click=' sayHello()'>

Click me

</

button

</

div

<

script

var

app

=

angular

.

module

(

'

myApp

'

,

[

]

)

;

app

.

controller

(

'

myCtrl

'

,

function

(

$

scope

)

{

$

scope

.

name

=

"

Runoob

"

; $

scope

.

sayHello

=

function

(

)

{

$

scope

.

greeting

=

'

Hello

'

scope

.

name

'

!

'

;

}

;

}

)

;

</

script

Try it »


Scope scope

Understanding the scope you are currently using is very important.

In the above two examples, there is only one scope, so it is relatively simple to handle, but in large projects, there are multiple scopes in the HTML DOM, and at this time you need to know which scope corresponds to the scope you are using.

AngularJS instance

When we useng-repeateach repeated item accesses the current repeated object:

<

div

ng-app=

"myApp"

ng-controller=

"myCtrl"

<

ul

<

li

ng-repeat=

"x in names"

{{x}}

<

/li

<

/ul

<

/div

<

script

var

app = angular.module(

'myApp'

, []);

app.controller(

'myCtrl'

,

function

($scope) {

    $scope.names = [

"Emil"

,

"Tobias"

,

"Linus"

];

});

<

/script

Try it »

Each

  • Elements can access the current repeated object, which corresponds to a string here, and use a variablexrepresents.


    Root scope

    All applications have a$rootScopewhich can apply tong-appamong all HTML elements in the directive.

    $rootScopecan be used throughout the entire application. It is a bridge between the scopes of various controllers. Values defined in rootscope can be used in various controllers.

    AngularJS instance

    When creating a controller, pass $rootScope as a parameter to be used in the application:

    <
    
    div
    
    ng-app=
    
    "myApp"
    
    ng-controller=
    
    "myCtrl"
    
    <
    
    h1
    
    {{lastname}}

    Family members:

    <
    
    <h1>
    
    <
    
    <ul>
    
    <
    
    <li>
    
    ng-repeat=

    "x in names"

    {{x}}

    {{lastname}}

    <
    
    /li
    
    <
    
    /ul
    
    <
    
    /div
    
    <
    
    script
    
    var

    app = angular.module(

    'myApp'

    , []);

    app.controller(

    'myCtrl'

    ,

    function

    ($scope, $rootScope) {

        $scope.names = [

    "Emil"

    ,

    "Tobias"

    ,

    "Linus"

    ];

    $rootScope.lastname =

    "Refsnes"

    ;

    });

    <

    /script

    Try it »