Part 33 Angular nested scopes and controller as syntax

本文详细介绍了在AngularJS中使用Controller As语法的优势。通过对比传统的$scope对象,Controller As语法使得代码更易于理解和维护,尤其是在处理嵌套控制器场景时。文章通过实例展示了如何在视图中直接引用控制器对象,从而简化了对属性的访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

转载于:https://www.cnblogs.com/gester/p/6535307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值