Working with nested scopes using $scope object : The following code creates 3 controllers  - countryController, stateController, and cityController. All of these have set name property on the $scope object.

var app = angular
            .module("Demo", [])
            .controller("countryController", function ($scope) {
                $scope.name = "India";
            })
            .controller("stateController", function ($scope) {
                $scope.name = "Maharashtra";
            })
            .controller("cityController", function ($scope) {
                $scope.name = "Mumbai";
            });

Now we want to display Country, State and City names as shown below. 

 

To get the output as shown above, we will have the following HTML in our view. name property retrieves the correct value as expected. However, the code is bit confusing.

<div ng-controller="countryController">
    {{name}}
    <div ng-controller="stateController">
        {{name}}
        <div ng-controller="cityController">
            {{name}}
        </div>
    </div>
</div>

Now let us say we want to display the names as shown below. 

 

To achieve this modify the HTML in the view as shown below. Notice we are using $parent to get the name property value of the immediate parent controller. To get the name property value of the grand parent, we are using $parent.$parent. This can get very confusing if you have many nested controllers and as a result the code gets less readable.

<div ng-controller="countryController">
    {{name}}
    <div ng-controller="stateController">
        {{$parent.name}} - {{name}}
        <div ng-controller="cityController">
            {{$parent.$parent.name}} - {{$parent.name}} - {{name}}
        </div>
    </div>
</div>

Let us see how things change when we use CONTROLLER AS syntax. First change the angular code to support CONTROLLER AS syntax. Notice we are not using $scope anymore with in our controllers, instead, we are using "this" keyowrd.

var app = angular
            .module("Demo", [])
            .controller("countryController", function () {
                this.name = "India";
            })
            .controller("stateController", function () {
                this.name = "Maharashtra";
            })
            .controller("cityController", function () {
                this.name = "Mumbai";
            });

With in the view, use CONTROLLER AS syntax. With this change, we are able to use the respective controller object and retrieve name property value. Now there is no need to juggle with $parent property. No matter how deep you are in the nested hierarchy, you can very easily get any controller object name property value. The code is also much readable now.

<div ng-controller="countryController as countryCtrl">
    {{countryCtrl.name}}
    <div ng-controller="stateController as stateCtrl">
        {{countryCtrl.name}} - {{stateCtrl.name}}
        <div ng-controller="cityController as cityCtrl">
            {{countryCtrl.name}} - {{stateCtrl.name}} - {{cityCtrl.name}}
        </div>
    </div>
</div>

Part 33 Angular nested scopes and controller as syntax的更多相关文章

  1. angular controller as syntax vs scope

    今天要和大家分享的是angular从1.2版本开始带来了新语法Controller as.再次之前我们对于angular在view上的绑定都必须使用直接的scope对象,对于controller来说我 ...

  2. Part 32 AngularJS controller as syntax

    So far in this video series we have been using $scope to expose the members from the controller to t ...

  3. Angular动态注册组件(controller,service...)

    使用angular的场景一般是应用类网站 这也意味着会有很多的controller,service,directive等等 正常情况下我们要把这些内容一次性下载并注册,由于文件较多,对首次加载的效率影 ...

  4. 10.1 Nested vectored interrupt controller (NVIC) 嵌套矢量中断控制器

    特点 60个可屏蔽中断通道(不包含16个Cortex™-M3的中断线): 16个可编程的优先等级(使用了4位中断优先级): 低延迟的异常和中断处理: 电源管理控制: 系统控制寄存器的实现: 1. 中断 ...

  5. 33.AngularJS 应用 angular.module定义应用 angular.controller控制应用

    转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 模块(Module) 定义了 AngularJS 应用. AngularJS 控制器(Co ...

  6. angular语法:Controller As

    这个东东我觉得很好哟. 数据可以在同一个页面的不同的controller之间自由穿梭... 当然, https://thinkster.io/a-better-way-to-learn-angular ...

  7. angular学习笔记(三十)-指令(10)-require和controller

    本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...

  8. angular 自定义指令 link or controller

    Before compilation? – Controller After compilation? – Link var app = angular.module('plunker', []); ...

  9. Angular中Controller之间的信息传递(第二种办法):$emit,$broadcast,$on

    $emit只能向parent controller传递event与data( $emit(name, args) ) $broadcast只能向child controller传递event与data ...

随机推荐

  1. AT2161-[ARC065D]シャッフル/Shuffling【dp】

    正题 题目链接:https://www.luogu.com.cn/problem/AT2161 题目大意 长度为\(n\)的\(0/1\)串,\(m\)个区间,你可以按照顺序任意排列区间中的数字,求最 ...

  2. bzoj3729-Gty的游戏【Splay,博弈论】

    正题 题目链接:https://darkbzoj.tk/problem/3729 题目大意 给出\(n\)个点的一棵树,第\(i\)个节点上有\(a_i\)个石子,然后每次可以选择不超过\(L\)个石 ...

  3. P5437-[XR-2]约定【拉格朗日差值,数学期望】

    正题 题目链接:https://www.luogu.com.cn/problem/P5437 题目大意 \(n\)个点的完全图,连接\(i,j\)的边权值为\((i+j)^k\).随机选出一个生成树, ...

  4. 计算机网络-4-2-ARP地址解析协议以及IP数据报不可变组成部分

    地址解析协议ARP ​ 在实际的应用中,我们会经常遇见这样的一个问题:我们已知一个机器(主机或者路由器的),我们怎么获取相应的硬件地址?,地址解析协议就是用来解决这个问题的. ARP协议的作用: 由上 ...

  5. 怒肝 Linux 学习路线,这回不难

    Linux 学习路线 by 鱼皮. 原创不易,请勿抄袭,违者必究! 大家好,我是鱼皮,又花 1 周肝出了 Linux 学习资料全家桶,包括学习路线.命令手册.视频.书籍.文档.实战教程.社区.工具.大 ...

  6. Jmeter使用问题记录

    Jmeter下载安装,设置中文,返回值乱码处理,下载接口测试 下载地址 解压后,在Jmeter的bin文件夹下启动 修改默认启动为中文简体:打开bin目录下的jmeter.properties文件,在 ...

  7. 在Vue中使用JSX,很easy的

    摘要:JSX 是一种 Javascript 的语法扩展,JSX = Javascript + XML,即在 Javascript 里面写 XML,因为 JSX 的这个特性,所以他即具备了 Javasc ...

  8. sql常用的统计公式

    hivesql中max,min函数不能作用于多列,因此在有上下门限区间限制时多用公式直接计算. max(x,y)=(x+y+ABS(x-y))/2 min(x,y)=(x+y-ABS(x-y))/2 ...

  9. Go语言核心36讲(Go语言进阶技术四)--学习笔记

    10 | 通道的基本操作 作为 Go 语言最有特色的数据类型,通道(channel)完全可以与 goroutine(也可称为 go 程)并驾齐驱,共同代表 Go 语言独有的并发编程模式和编程哲学. D ...

  10. 【c++ Prime 学习笔记】第4章 表达式

    表达式由一个或多个运算对象组成,对表达式求值返回结果. 字面值和变量是最简单的表达式 把运算符和运算对象组合可得到复杂表达式. 4.1 基础 4.1.1 基本概念 一元运算符作用于一个对象,如取地址符 ...