<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Richard Ramsden on Medium]]></title>
        <description><![CDATA[Stories by Richard Ramsden on Medium]]></description>
        <link>https://medium.com/@rramsden?source=rss-3bf368435259------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MxbgLy-9WSNXEG9N.jpeg</url>
            <title>Stories by Richard Ramsden on Medium</title>
            <link>https://medium.com/@rramsden?source=rss-3bf368435259------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 17:53:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rramsden/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular Scopes]]></title>
            <link>https://medium.com/@rramsden/angular-scopes-777929114e19?source=rss-3bf368435259------2</link>
            <guid isPermaLink="false">https://medium.com/p/777929114e19</guid>
            <dc:creator><![CDATA[Richard Ramsden]]></dc:creator>
            <pubDate>Sun, 02 Feb 2014 10:16:58 GMT</pubDate>
            <atom:updated>2014-04-03T01:14:25.135Z</atom:updated>
            <content:encoded><![CDATA[<h4>Beginning AngularJS</h4><p>One of the most important topics in Angular are scopes. Its definitely worth your time to get a solid understanding of how they work since they are used extensively throughout development in AngularJS applications.</p><h4>What is a scope?</h4><p>In computer programming, a scope is an isolated namespace where we can assign values and expressions. In AngularJS you have a <strong>$scope</strong> object which you can assign key/value pairs too. These values become immediately accessible in your application controllers and views.</p><p>Often scopes are created when declaring directives in Angular. For example, when we create a controller using the <strong>ng-controller</strong> directive were also creating a new scope for that controller to use. For example,</p><pre>&lt;div ng-controller=&quot;DogeCtrl&quot;&gt;<br>    &lt;div&gt;{{ wow }}&lt;/div&gt;<br>    &lt;div&gt;{{ such }}&lt;/div&gt;<br>    &lt;div&gt;{{ very }}&lt;/div&gt;<br>&lt;/div&gt;</pre><p>The <strong>ng-controller </strong>directive will create a new scope around the <em>&lt;div ng-controller&gt; </em>DOM<em> </em>element and in this example look for a controller called <strong>DogeCtrl </strong>and instantiate it with a new <strong>$scope</strong> object.</p><pre>function DogeCtrl($scope) {<br>    $scope.wow = &quot;wow&quot;;<br>    $scope.such = &quot;such scope&quot;;<br>    $scope.very = &quot;very angular&quot;;<br>}</pre><p>When this controller is instantiated from the view it has a <strong>$scope</strong> object that is associated with that DOM element where ng-controller was declared. In this example we can assign values <em>{{ wow }}, {{ such }}, and {{ very }} </em>and Voilà!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/198/1*tKThsJqOZIigUGIzQQ_Ebg.png" /></figure><p>See the <a href="http://jsfiddle.net/rramsden/nLR9C/">jsfiddle example</a> for a more detailed example</p><p><em>Changing a value and having it update like this is referred to as </em><strong><em>Two-way data binding</em></strong><em> .</em></p><h4><strong>Two-way Data Bindings</strong></h4><p>Two-way data binding<strong> </strong>is the ability to bind a value to an element in the UI and vice-versa. When you reference values in your view you create a binding on $scope.{{ some key }}. Updating that value immediately updates the view. This gives Angular its dynamic “real-time” feeling.</p><p>Lets get a better idea how this works. Will create a two-way binding on <strong>$scope.world</strong> in the example below</p><pre>&lt;div ng-controller=“HelloCtrl”&gt;<br>    &lt;h1&gt;Hello {{ world }}&lt;/h1&gt;<br>    &lt;input type=“text” ng-model=“world”&gt;<br>&lt;/div&gt;<br> <br>function HelloCtrl($scope) {<br>    $scope.world = “World”;<br>} </pre><p>Running this code you can see that <strong>$scope.world</strong> is changed when the user enters a value for the input field.</p><h4><strong>Scope Hierarchy</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*PXzUBFumudv4vx77lj5sCA.png" /><figcaption>Every scope inherits from $rootScope</figcaption></figure><p>Scopes in angular are arranged in a hierarchical structure similar to the DOM. The top level element in this structure is the <strong>$rootScope </strong>and is bound to the DOM element where <strong>ng-app </strong>is declared.</p><p>Let’s take a look at an example demonstrating how this hierarchy works</p><pre>&lt;div ng-app ng-init=&quot;message = &#39;test message&#39;&quot;&gt;<br>    &lt;h2&gt;$rootScope: {{ message }}&lt;/h2&gt;<br>    &lt;div ng-controller=&quot;ExampleCtrl&quot;&gt;<br>        &lt;p&gt;Child Scope: &lt;b&gt;{{ message }}&lt;/b&gt;&lt;/p&gt;<br>        &lt;p&gt;Change value: &lt;input type=&quot;text&quot; value=&quot;change me&quot; ng-model=&quot;message&quot;/&gt;&lt;/p&gt;<br>    &lt;/div&gt;<br>&lt;/div&gt; </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*s3sBkqrhgwpWRKzHxBf9XA.png" /></figure><p>In this example we can see that <strong>message</strong> is defined on the<strong> $rootScope</strong> using <strong>ng-init. </strong>If we try changing that value in ExampleCtrl it won’t update the value in the <strong>$rootScope</strong>. Why?</p><p>Changes to a scope are only visible to the scope itself and its children. When we changed the value of <strong>message</strong> in ExampleCtrl it re-defined <strong>$scope.message </strong>on itself and not its parent.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=777929114e19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>