更新父母或者其他同步角控制器的最佳实践? [英] Best practice to update parent or other simultaneous Angular controllers?

查看:139
本文介绍了更新父母或者其他同步角控制器的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅,如果这是一般,但是我正在寻找一种方法,我是新来的角。说我有一个主控制器和一个运行时创建的模式控制器。让他们成为WidgetCtrl和NewWidgetModalCtrl。

Forgive if this is general, but I'm looking for an approach and I'm new to Angular. Say I have a main controller and a runtime created modal controller. Let them be WidgetCtrl and NewWidgetModalCtrl.

我想创建一个Widget,并使用 $ resource.save()将数据发送到服务器。它会返回一些数据,我们关闭了新的Widget模态(它被摧毁$)。如何将这个小部件添加到列表中WidgetCtrl 的?

I want to create a Widget and send that data to the server using $resource.save(). It returns some data and we close the "New Widget Modal" (it gets $destroyed). How would I add this widget to the list in WidgetCtrl?

有人说使用共享服务/工厂。但问题是,这些都是单身。我可能要小工具的两个单独的列表。我可能要在车列表和一个受欢迎的列表中的小部件。如果我使用一个单,我pretty确保其静态列表翻过所有控制器。我不得不重复非常相似的类具有相同的结构,但不同的数据。 ....?它非常适用类似的看到在这个例子中会话:模型共享两个控制器之间 ,但没有很好的东西,像浏览和同类型对象的各种列表中选择。

Some say to use a shared service/factory. But the problem is that these are singletons. I may want two separate lists of Widgets. I might want a widgets in the cart List and a popular List. If I use a singleton, I'm pretty sure its a static list accross all controllers. I'd have to duplicate very similar classes to have same structured but dissimilar data. ....? It works well for something like a Session as seen in this example: Model Shared Between Two Controllers but not well for something like browsing and selecting from various lists of same-typed objects.

我怎么能有抽象小部件的定义,和控制器之间建立和控制列表?该角的讨论似乎考虑的父$范围的修改的X控制器通信对象一种不好的做法。这是在这种情况下加倍糟糕,因为我可以有任何出现的一些情态动词的。

How can I have abstract Widget definition, and build and control lists between controllers? The Angular discussions seem to consider x-controller communication by modification of parent $scope objects a bad practice. And this is doubly bad in this situation because I can have any number of modals appearing.

有人说做$放出,然后在父范围刷新数据。但后来这将导致所有数据从服务器获取,当所有我们所做的就是添加一个小部件重载。

Some say to do $emit and then refresh the data in the "parent" scope. But then this causes all the data to get reloaded from the server, when all we did was add a single widget.

   $scope.$watch( 'widgets', function(){ 
        this.widgets = Widget.query(); //get all widgets
    });

我猜我正在寻找在这里是一个扩展,可以在两个位置被引用,并通过$资源或应用添加到基础模型的定义,而不是一个单独谁总是更新任何一个实例变量该类型的列表。我只是在这里糊涂了,我要继续前进,使一(厂)PopularWidgetModel和(工厂)CartWidgetModel?似乎有些单调乏味。也许我不太明白如何这个将适用于我的情况。

推荐答案

我有点困惑,但我会在一个答案采取刺​​伤。

I'm a little confused, but I'll take a stab at an answer.

当小部件数据来自$救回来,您可以使用。那么()将它添加到$ scope.widgets在widgetCtrl。如果你想分享控制器之间列表(即有没有海誓山盟层次关系),这件列表应该是你注入到每个控制器的服务。然后,每个控制器将有

When the widget data comes back from $save, you can use .then() to add it to $scope.widgets in widgetCtrl. If you wanted to share this list between controllers (that have no hierarchical relationship to eachother), this widget list should be in a service that you inject into each controller. Each controller will then have

$scope.widgets = widgetService.widgetList;

在这里的widgetList是由您调用$资源的功能更新。

where widgetList is updated by your calls to $resource functions.

任何你想显示部件的这份名单中,你可以使用NG重复=的widget小工具。

Anywhere you want to show this list of widgets, you would use ng-repeat="widget in widgets".

如果你有不同的列表,就像一个车,一个流行的,那么你可能会找到一种方法,包括inCart或inPopular作为你的widget对象的属性。然后你可以使用

If you have different lists, like one for cart and one for popular, then you might find a way to include "inCart" or "inPopular" as properties in your widget object. Then you can use

<div ng-repeat="widget in widgets" ng-if="widget.inCart">

<div ng-repeat="widget in widgets" ng-if="widget.inPopular">

<div ng-repeat="widget in widgets | filter:{inCart:true}">

和等。我不是语法的过滤器100%肯定,但类似的东西可以做到的。

and so on. I'm not 100% sure on the syntax for the filter, but something like that can be done.

我不认为你需要在这里的两个工厂,因为它是相同的数据;但就像你说的,你可能想有显示它以不同的方式,或部分列表这里,部分列表。

I don't think you need two factories here because it's the same data; but like you say, you might want to show it in different ways, or partial list here, partial list there.

此外,指令可以帮助这里,因为它听起来就像你希望有一个抽象的小部件的定义,而对我来说,这听起来像一个指令。他们有自己的控制器和可以有一个孤立的范围,或者很容易地分享他们的父控制器的范围为使用$ scope.parentVar。

Also, directives might help here because it sounds like you want a "abstract widget definition", and to me, that sounds like a directive. They have their own controllers and can either have an isolated scope, or share the scope of their parent controller just as easily as using $scope.parentVar.

希望有所帮助。

这篇关于更新父母或者其他同步角控制器的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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