In this video we will discuss the difference between $scope and $rootScope. The main difference is that, $rootScope is available globally (for all controllers), whereas $scope is only available to the controller that has created it and it's children. 

Let us understand this with an example.

Controller Code : We have 2 controllers (redColourController & greenColourController). redColourController has set redColour property on $scope and rootScopeColour on $rootScope. This means redColour property cannot be used outside the redColourController, where as rootScopeColour that is set on $rootScope can be used anywhere. greenColourController has set greenColour property on $scope. This means greenColour property cannot be used outside the greenColourController

var app = angular
            .module("Demo", [])
            .controller("redColourController", function ($scope, $rootScope) {
                $rootScope.rootScopeColour = "I am Root Scope Colour";
                $scope.redColour = "I am Red Colour";
            })
            .controller("greenColourController", function ($scope) {
                $scope.greenColour = "I am Green Colour";
            })

View HTML : 

<div ng-controller="redColourController">
    Root Scope Colour : {{rootScopeColour}} <br />
    Red Colour Controller : {{redColour}} <br />
    Green Colour Controller :
    <span style="color:red" ng-show="greenColour == undefined">
        greenColour is undefined
    </span>
</div>
 
<br />
 
<div ng-controller="greenColourController">
    Root Scope Colour : {{rootScopeColour}} <br />
    Green Colour Controller : {{greenColour}} <br />
    Red Colour Controller :
    <span style="color:red" ng-show="redColour == undefined">
        redColour is undefined
    </span>
</div>

Output : From the output it is clear that the rootScopeColour property that is set on $rootScope is available for both the controllers (redColourController & greenColourController). Where as redColour property set on $scope is available only for redColourController and not for greenColourController. Similarly, greenColour property set $scope is available only for greenColourController and not redColourController. Hope this example has made the difference between $rootScope and $scope clear.

Part 37 Difference between $scope and $rootScope的更多相关文章

  1. Angular $scope和$rootScope事件机制之$emit、$broadcast和$on

    Angular按照发布/订阅模式设计了其事件系统,使用时需要“发布”事件,并在适当的位置“订阅”或“退订”事件,就像邮箱里面大量的订阅邮件一样,当我们不需要时就可以将其退订了.具体到开发中,对应着$s ...

  2. AngularJS进阶(二十一)Angularjs中scope与rootscope区别及联系

    Angularjs中scope与rootscope区别及联系 scope是html和单个controller之间的桥梁,数据绑定就靠他了.rootscope是各个controller中scope的桥梁 ...

  3. ng $scope与$rootScope的关系

    $scope与$rootScope的关系:①不同的控制器之间 是无法直接共享数据②$scope是$rootScope的子作用域对象$scope的id是随着控制器的加载顺序依次递增,$rootScope ...

  4. Angular $scope和$rootScope

    <!DOCTYPE html><html ng-app='myModule'><head lang="en"> <meta charset ...

  5. angular源码分析:angular中$rootscope的实现——scope的一生

    在angular中,$scope是一个关键的服务,可以被注入到controller中,注入其他服务却只能是$rootscope.scope是一个概念,是一个类,而$rootscope和被注入到cont ...

  6. (九)通过几段代码,理清angularJS中的$injector、$rootScope和$scope的概念和关联关系

    $injector.$rootScope和$scope是angularJS框架中比較重要的东西,理清它们之间的关系,对我们兴许学习和理解angularJS框架都很实用. 1.$injector事实上是 ...

  7. AngularJS的$rootScope和$scope联系和区别

    scope是html和单个controller之间的桥梁,数据绑定就靠他了. rootscope是各个controller中scope的桥梁.用rootscope定义的值,可以在各个controlle ...

  8. Angular作用域的层级概念(scope)

    首先引入 angular 的根作用域:$rootScope ng-app:定义了angualr的作用域 ng-controller:定义了控制器 $scope定义了视图与控制器之间的纽带,而scope ...

  9. AngularJS 深入理解 $scope

    $scope 的使用贯穿整个 AngularJS App 应用,它与数据模型相关联,同时也是表达式执行的上下文.有了$scope 就在视图和控制器之间建立了一个通道,基于作用域视图在修改数据时会立刻更 ...

随机推荐

  1. P3426-[POI2005]SZA-Template【KMP】

    正题 题目链接:https://www.luogu.com.cn/problem/P3426 题目大意 给出一个长度为\(n\)的字符串\(s\),求一个长度最小的字符串\(t\)使得\(s\)所有\ ...

  2. CF710F-String Set Queries【AC自动机,二进制分组】

    正题 题目链接:https://www.luogu.com.cn/problem/CF710F 题目大意 \(T\)次操作 往集合中加入一个字符串 往集合中删除一个字符串 给出一个模式串求出现的集合里 ...

  3. "错误: 找不到或无法加载主类"解决办法

    前言:记上次一个找了个把小时的问题(很烦这些配置) 原因: 从svn下checkOut的项目 在application的配置的输出class路径为main,而class文件路径是在项目名的根路径下,所 ...

  4. v-for为什么最好(一定)要加key

    v-for 指令基于一个数组来渲染一个列表,如下 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> ...

  5. Vulnstack内网靶场1

    最近发现了一个内网的靶场网站,简单配置一下网络就可以,不用自己搭建这么麻烦漏洞信息 (qiyuanxuetang.net) 环境配置 内网1靶场由三台机器构成:WIN7.2008 server.200 ...

  6. Java-多态(上)

    什么是多态 同一方法可以根据发送对象的不同而采取多种不同的行为方式 一个对象实际类型是确定的 但指向其引用类型却有很多 注意事项 多态是方法的多态 属性没有多态 父类和子类 有联系 类型转换异常 Cl ...

  7. 从零入门 Serverless | 一文详解 Serverless 技术选型

    作者 | 李国强 阿里云资深产品专家 今天来讲,在 Serverless 这个大领域中,不只有函数计算这一种产品形态和应用类型,而是面向不同的用户群体和使用习惯,都有其各自适用的 Serverless ...

  8. git 更新与图形界面

    git 软件更新:git update-git-for-windows 或者 git update gitk 是一个历史记录的图形化查看器. 使用:只需 cd 到一个 Git 仓库,然后键入:gitk ...

  9. 「软件测试实战教程系列(三)」弃繁就简,接口测试神器Postman|收藏版

    软件测试实战教程系列(三)弃繁就简,接口测试神器Postman|收藏版 Postman主要帮我们干了三件事: 1.把相关集合放到一个集合当中方便管理. 2.对指定接口发送请求. 3.断言 下面我们使用 ...

  10. logging的基本使用

    logging模块打印log的时候主要有一下几个,级别顺序:CRITICAL>ERROR>WARNING>INFO>DEBUG: 1.日志输出到file: import log ...