简体   繁体   English

AngularJS:如何在两个隔离的控制器和共享服务之间创建双向数据绑定?

[英]AngularJS : How to create a two-way data binding between two isolated controllers and a shared service?

I am trying to create a two-way data binding between two isolated controllers and a shared service (which provides another isolated scope): 我试图在两个隔离的控制器和一个共享服务(提供另一个隔离的范围)之间创建双向数据绑定:

app.factory("sharedScope", function($rootScope) {
    var scope = $rootScope.$new(true);
    scope.data = "init text from factory";
    return scope;
});

app.controller("first", function($scope, sharedScope) {
    $scope.data1 = sharedScope.data;
});

app.controller("second", function($scope, sharedScope) {
    $scope.data2 = sharedScope.data;
});

Fiddle: http://jsfiddle.net/akashivskyy/MLuJA/ 小提琴: http//jsfiddle.net/akashivskyy/MLuJA/

When the application launches, data1 and data2 are correctly updated to the init text from factory , but later, if I change any of them, those changes are not reflected throughout those three scopes. 当应用程序启动时, data1data2init text from factory正确更新为init text from factory ,但稍后,如果我更改其中任何一个,那么这些更改不会反映在这三个范围中。

How can I bind them? 我该如何绑定它们?

PS If there's a better way than returning a scope and still having access to event and observing functionalities (without basically re-writing them), let me know. PS如果有一个更好的方法,而不是返回一个范围,仍然可以访问事件和观察功能(基本上不重写它们),请告诉我。 :) :)

Fixed it. 固定它。 References will be lost if you are using primitives, as in your fiddle. 如果你使用原语,就会丢失引用,就像你的小提琴一样。

Check this: 检查一下:

Updated fiddle 更新了小提琴

app.factory("sharedScope", function($rootScope) {
    var scope = $rootScope.$new(true);
    scope.data = {text: "init text from factory"};
    return scope;
});

app.controller("first", function($scope, sharedScope) {
    $scope.data1 = sharedScope.data;
});

app.controller("second", function($scope, sharedScope) {
    $scope.data2 = sharedScope.data;
});

Yet another fun bit: In this case, you don't need to inject $scope or $rootScope. 还有一点有趣的是:在这种情况下,您不需要注入$ scope或$ rootScope。 The following code works if you utilize Controller As. 如果您使用Controller As,以下代码可以正常工作。 Check the Fiddle 检查小提琴

var app = angular.module("app", []);

app.factory("sharedScope", function() {
    var _this = this;
    _this.data = {text: "init text from factory"};
    return _this;
});

app.controller("first", function(sharedScope) {
    var _this = this;
    _this.data1 = sharedScope.data;
});

app.controller("second", function(sharedScope) {
    var _this = this;
    _this.data2 = sharedScope.data;
});

For even more fun, consider controllers, services, and factories as classes. 为了获得更多乐趣,请将控制器,服务和工厂视为类。 More Fiddles 更多小提琴

var app = angular.module("app", []);

var SharedScope = function(){
    var _this = this;
    _this.data = {text: "init text from factory"};
    return _this;
};

app.factory("sharedScope", SharedScope);

var First = function(sharedScope){
    var _this = this;
    _this.data1 = sharedScope.data;
};

var Second = function(sharedScope){
    var _this = this;
    _this.data2 = sharedScope.data;
};

First.$inject = ['sharedScope'];
Second.$inject = ['sharedScope'];

app.controller("first", First);              
app.controller("second", Second);

I've been playing at implementing Josh Carroll's Guidelines to Avoid "Scope Soup" 我一直在玩Josh Carroll的避免“范围汤”指南

JavaScript passes objects by reference, so all scopes will point to the same object. JavaScript通过引用传递对象,因此所有范围都将指向同一个对象。 Why not just do this? 为什么不这样做呢?

app.factory("sharedData", function() {
    return {data: "init text from factory"};
});

app.controller("first", function($scope, sharedData) {
    $scope.sharedData = sharedData;
});

app.controller("second", function($scope, sharedData) {
    $scope.sharedData = sharedData;
});

and in your view: 在你看来:

<p>{{sharedData.data}}</p>

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

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