Angularjs:分享控制器之间的观察数据 [英] Angularjs: Share observable data between controllers

查看:138
本文介绍了Angularjs:分享控制器之间的观察数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用serveral的控制器,一个DataContext。结果
在DataContext可以以不同的方式改变(由Ajax调用,用户/视图,通过服务器)。结果
当在DataContext中的数据已经改变使用DataContext的所有控制器应得到通知。

I need to use a datacontext in serveral controllers.
The datacontext can be change in different ways (by ajax call, by user/view, by server).
All controllers using the datacontext should be notified when the data in the datacontext has changed.

的DataContext:

datacontext:

var userDataContext = { firstName: "John", lastName: "Doe" };

我在每一个例子模拟了setTimeout的结果。

I simulated in every example a backend change with setTimeout

setTimeout(function() {
    console.log("change firstname");
    userContext.firstName= "Bart";
}, 3000);


下面是在小提琴例子淘汰赛,它工作正常。结果
http://jsfiddle.net/8ct0L1zu/4/

我也试着做它的,但它的部分工作。

I also tried to do it with Angular but it partially work.


  • 当我通过完整的对象到控制器(引用),改变控制器之间的作品。结果
    当改变在JavaScript /后端DataContext的,它不会改变我的看法。结果

  • When I pass the complete object to the controller(by reference), changes works between controllers.
    When changing the datacontext in javascript/backend, it doesn't change my views.

我也preFER有只在controlers范围名字和姓氏,并没有完整的对象。结果但在这种情况下,没有什么作品。结果
原因:在我们的应用会有很多datacontexts的大对象数组结果

I also prefer to have only the firstname and lastname in the controlers scope, and not the complete object.
But in this case, nothing works.
Reason: In our application there will be a lot of datacontexts with arrays of big objects.

http://jsfiddle.net/19xv3skn/1/

要真正使工作,我用的与角淘汰赛。角的数据绑定和淘汰赛的DataContext的。这工作得很好,但我真的不喜欢这个解决方案。结果
http://jsfiddle.net/7chp5xLa/2/

To really make it work, I used Angular with knockout. Angular for databinding and knockout for datacontext. This works fine, but I don't really like this solution.
http://jsfiddle.net/7chp5xLa/2/

是否有一个最佳实践或更好的方式与角度观察到的一个的DataContext工作?

推荐答案

我认为这个问题可能是你想在你的AngularJS应用管理数据的方式。您在AngularJS生命周期之外编辑的UserContext数据。相反,管理什么AngularJS看到的,你给它AngularJS后编辑数据。相反,考虑从内部角度改变的UserContext状态下,在你需要它

I think the issue might be the way you think about managing data in your AngularJS app. You are editing the userContext data outside of the AngularJS lifecycle. Instead of managing what AngularJS 'sees', you edit the data after giving it to AngularJS. Instead, consider changing the userContext state from within Angular, there where you need it

在你的榜样,你有两个控制器,对$范围相同的数据,也许你应该指的是管理数据为$范围,而不是一个共同的父控制器:

In your example, you have two controllers that have the same data on $scope, maybe you should refer to a common parent controller that manages the data for the $scope instead:

的jsfiddle

app.controller('ParentCtrl', function ($scope, userContext, $timeout) {
   $scope.firstName = userContext.firstName;
   $scope.lastName = userContext.lastName;
   $scope.user = userContext;
   $timeout(function() {
       console.log("change firstname");
       userContext.firstName= "Bart";
   }, 3000);
});

当你想要做不同的事情,并与每个控制器的UserContext

事情变得更有趣。在这种情况下,您可能需要根据自己的需要控制器之间的一些同步。有许多回答有关控制器之间共享状态和控制器之间的通信问题。

Things get more interesting when you want to do different things to and with userContext per controller. In that case you may need some synchronization between the controllers depending on your needs. There are numerous answers to questions about sharing state between controllers and communicating between controllers.

这篇关于Angularjs:分享控制器之间的观察数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆