加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJs:不同页面上的同一个控制器是否共享范围?

发布时间:2020-12-17 17:21:12 所属栏目:安全 来源:网络整理
导读:我是学习 Angularjs的新手,有点困惑.我想问一下,如果相同的控制器在不同的页面绑定,那些页面是否共享相同的范围变量,或者它们有自己的隔离范围?请记住,两个页面都使用相同的控制器. 解决方法 I want to ask that if the same controller is binded at diffe
我是学习 Angularjs的新手,有点困惑.我想问一下,如果相同的控制器在不同的页面绑定,那些页面是否共享相同的范围变量,或者它们有自己的隔离范围?请记住,两个页面都使用相同的控制器.

解决方法

I want to ask that if the same controller is binded at different pages does those pages share the same scope variable or they have their own isolated scope? Remember both of the pages are using the same controller.

是的,我在这里回应其他人的想法.如果您对任意数量的页面使用相同的控制器,则每个页面的范围将保持不变.除非不更改范围,否则该值将保持初始化期间的值.

例如.你的控制器如下

myApp.controller('FirstCtrl',function( $scope){
    $scope.myVar = 'this is my scope';
});

如果您对两个页面使用相同的控制器,那么第一页和第二页将具有相同的myVar值.因此,在一页中的html下面

<div ng-model="myVar"></div>

在第二页的html下面

<span ng-model="myVar"></span>

将显示为

<div ng-model="myVar">this is my scope</div>

<span ng-model="myVar">this is my scope</span>

分别.

鉴于此,我想补充说,也可以使用$emit,$broadcast和$on在不同的控制器之间共享$scope.
在http://www.dotnet-tricks.com/Tutorial/angularjs/HM0L291214-Understanding- $emit,– $broadcast-and- $on-in-AngularJS.html上阅读更多相关信息

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读