zoukankan      html  css  js  c++  java
  • Part 37 Difference between $scope and $rootScope

    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. 

  • 相关阅读:
    了解 NoSQL 的必读资料
    关于什么时候用assert(断言)的思考
    这次见到了一些大侠
    NetBeans 时事通讯(刊号 # 87 Jan 12, 2010)
    动态链接库dll,静态链接库lib, 导入库lib
    新女性十得 写得了代码,查得出异常
    记录系统乱谈
    新女性十得 写得了代码,查得出异常
    fullpage.js禁止滚动
    RunningMapReduceExampleTFIDF hadoopclusternet This document describes how to run the TFIDF MapReduce example against ascii books. This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (110 nodes) Google Pro
  • 原文地址:https://www.cnblogs.com/gester/p/6535463.html
Copyright © 2011-2022 走看看