Giter Site home page Giter Site logo

angular-scope-rootscope-readme's Introduction

$scope and $rootScope

Overview

Objectives

  • Describe what a $scope is
  • Describe what $rootScope is
  • Describe how Angular utilises Prototypal Inheritance
  • Create a $scope variable
  • Bind the $scope variable to the View

$scope

You might've noticed the magic of $scope in the last lesson - an object that we can assign values to and they appear in our view. $scope is named by Angular - we can't change the name of it, so make sure you refer to it as $scope!

Angular introduces scopes into our application. Whenever we have a new instance of a controller, that controller has a $scope. Everything inside our controller can access this scope, however anything outside cannot. $scopes can access their parent scope.

Using $scope in our views

We can access every property of $scope that we set in the controller inside our views. This is done using the {{ }} syntax that we used in our first lab. All we need to do is put the property key inside of the double curlys - such as for $scope.name, we use {{ name }}. We can also use . to access properties inside properties (for example, an object).

For example:

function MainController($scope) {
  $scope.contact = {
    name: 'Bill Gates',
    phone: '01234567890'
  };
  
  $scope.year = '2016';
}
<div ng-controller="MainController">

  <h2>{{ contact.name }}</h2>
  <h4>{{ contact.phone }}</h4>

  <div>
    Copyright {{ year }}.
  </div>
</div>

This would render out:

<div ng-controller="MainController">

  <h2>Bill Gates</h2>
  <h4>01234567890</h4>

  <div>
    Copyright 2016.
  </div>
</div>

Neat, huh?

However, if we tried to access items outside of our controller, such as:

<div ng-controller="MainController">

  <h2>{{ contact.name }}</h2>
  <h4>{{ contact.phone }}</h4>

  <div>
    Copyright {{ year }}.
  </div>
</div>

{{ year }} {{ name }}

All we'd get rendered is:

<div ng-controller="MainController">

  <h2>Bill Gates</h2>
  <h4>01234567890</h4>

  <div>
    Copyright 2016.
  </div>
</div>

{{ year }} {{ name }}

Angular Scopes

Look at the example above - each red rectangle is a scope. If we look at the first rectangle for the HTML element with ng-controller - this is currently what we have in our application. From this $scope, we'd be able to access the parent $scope (the big rectangle around the whole image), but not the $scope of the second HTML element with ng-controller.

$rootScope

We also have the $rootScope - this is the first scope in our application. All of our $scope's will end up linking to this scope eventually. Not to worry about this yet, you'll come to see it a lot more later on.

What else does $scope offer?

$scope isn't just an object that can assign values to the view - it's also an extremely powerful API that allows us to do things, such as communicating between scopes, keep track of data state, watching for changes and much more - but we'll touch on this later.

View angular-scope-rootscope-readme on Learn.co and start learning to code for free.

angular-scope-rootscope-readme's People

Contributors

ipc103 avatar toddmotto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.