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

前端之家收集整理的这篇文章主要介绍了AngularJs:不同页面上的同一个控制器是否共享范围?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是学习 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上阅读更多相关信息

希望这可以帮助.

猜你在找的Angularjs相关文章