简体   繁体   English

如何在AngularJS中与ng-app共享作用域变量

[英]how to share a scope variable with ng-app's in AngularJS

I have two different ng-app running on same page. 我在同一页面上运行了两个不同的ng-app。

  1. bookingApp bookingApp
  2. calendarApp calendarApp

I want to share selected date scope variable from calendarApp to bookingApp. 我想共享从calendarApp到bookingApp的选定日期范围变量。 How I can achieve this functionality? 如何实现此功能?

Thanks, Gladiator 谢谢,角斗士

You have to use angularjs service for this purpose.Hope the following implementation will help you to get some understanding 为此,您必须使用angularjs服务。希望以下实现可以帮助您了解一些

angular.module('app.A', [])
.service('ServiceA', function() {
    this.getValue = function() {
        return this.myValue;
    };

    this.setValue = function(newValue) {
        this.myValue = newValue;
    }
});

angular.module('app.B', ['app.A'])
.service('ServiceB', function(ServiceA) {
    this.getValue = function() {
        return ServiceA.getValue();
    };

    this.setValue = function() {
        ServiceA.setValue('New value');
    }
});

You can share data between Angular apps by Window postMessage and / or localStorage . 您可以通过Window postMessage和/或localStorage在Angular应用之间共享数据。

Just wrap postMessave into Angular Service and propagate changes on post event. 只需将postMessave包装到Angular Service中,并在发布事件后传播更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM