简体   繁体   English

我如何制作两个页面AngularJS共享相同的$ scope?

[英]How can I make two pages AngularJS share the same $scope?

I want to use angularJS in my web site. 我想在我的网站上使用angularJS。 But I want to use it in several pages. 但我想在几页中使用它。

The problem is when someone tried to access these pages, they behave like two apps since $scope is different to these apps. 问题是当有人试图访问这些页面时,他们的行为就像两个应用程序,因为$ scope与这些应用程序不同。 But I want to use these pages as a same app by sharing the same memory space of angularJS. 但我希望通过共享angularJS的相同内存空间将这些页面用作相同的应用程序。

How can I do that? 我怎样才能做到这一点?

It's not possible for entirely different page loads. 完全不同的页面加载是不可能的。

But after all $scope means some data. 但毕竟$ scope意味着一些数据。 You want to enable that data for the other pages as well. 您也希望为其他页面启用该数据。

One thing you can do is when you switch between pages you can transfer the data ($scope) into the other page and that app(page) can initialize that data. 您可以做的一件事是当您在页面之间切换时,您可以将数据($ scope)传输到另一个页面,并且该应用程序(页面)可以初始化该数据。 Then it'll work as sharing the same $scope. 然后它将作为共享相同的$范围工作。 But this will be poor approach if you switch between pages frequently. 但如果您经常在页面之间切换,这将是一个糟糕的方法。

General answer is NO. 一般答案是否定的。 But, 但,

The normal flow of a browser receiving an event is that it executes a corresponding JavaScript callback. 接收事件的浏览器的正常流程是它执行相应的JavaScript回调。 Once the callback completes the browser re-renders the DOM and returns to waiting for more events. 一旦回调完成,浏览器将重新呈现DOM并返回以等待更多事件。

So, after you redirected to a pages which doesn't use any of this $scope or child scope, javascript objects (scope models) to be reclaimed by the garbage collector. 因此,在重定向到不使用任何此范围或子范围的页面后,垃圾收集器将回收javascript对象(范围模型)。

So, there is not possible simple way to do that. 因此,没有简单的方法可以做到这一点。 Except the parameter passing to the new scope while page is redirected. 除了在重定向页面时传递到新范围的参数。

But if you find ways to stop the $scope.destroy() call from scope creator and link that DOM to the other page's $scope. 但是如果你找到了从范围创建者停止$ scope.destroy()调用的方法,并将该DOM链接到另一个页面的$ scope。 Conceptually that is possible (But I don't know how to do that). 从概念上讲这是可能的(但我不知道该怎么做)。 If you are desperate to use angular in that approach, then you can do some research on that. 如果你迫切希望在这种方法中使用角度,那么你可以对此进行一些研究。

Otherwise pass the $scope parameter or required fields of the $scope to the other app. 否则将$ scope参数或$ scope的必填字段传递给另一个应用程序。 That'll do what you want. 那就做你想要的。 But as @Simon57 says; 但正如@ Simon57所说; it'll be poor approach if you switch between apps frequently. 如果你经常在应用程序之间切换,这将是一个糟糕的方法。

More: http://docs.angularjs.org/guide/scope 更多: http//docs.angularjs.org/guide/scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在AngularJS中有两个具有相同ng-controller的div,我怎样才能让它们共享信息? - I have two divs with the same ng-controller in AngularJS, how can I make them share information? 如何使用AngularJS中的相同控制器在视图之间共享变量? - How can i share variables between views, with the same controller in AngularJS? 如何使用两个不同的ui路由器在angularjs中的状态之间共享作用域数据? - How do I share scope data between states in angularjs using two different ui routers? 如何在angularjs的两个控制器之间共享$ scope属性,而不是其值? - How to share $scope properties, not its value between two controllers in angularjs? 我可以使作用域扩展angularjs中的对象吗? - Can I make scope extend an object in angularjs? 如何将表单提交到两个不同的页面(不是同时)? - How can I submit a form to two different pages (not at same time)? 如何在angularJS中访问其他$ scope - How can I access different $scope in angularJS 我如何在两个之间共享数据 <ons-template> ?使用angularjs和onsen ui - how can I share data between two <ons-template> ?use angularjs and onsen ui 如何使用angularJS动态加载页面? - How can i load pages dynamically with angularJS? 如何使用angularjs在同一屏幕上调用四个不同的页面? - How can I call four different pages in same screen using angularjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM