是否有更新从一个动态创建的视图/控制器父指示一个松耦合方式,是父母的孩子 [英] Is there a loosly coupled way to update a parent directive from a dynamically created view/controller that is a child of the parent

查看:104
本文介绍了是否有更新从一个动态创建的视图/控制器父指示一个松耦合方式,是父母的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有窗口实例。该应用程序可以包含多个窗口和窗口可以包含多个视图。该意见是每个窗口实例的孩子。窗户和视图的创建者是指令与一个孤立的范围。我要的意见被松耦合到它们的父窗口,而不必像$范围。$父

In my app I have window instances. The app can contain multiple windows and windows can contain multiple views. The views are children of each window instance. The windows and view creator are directive with an isolated scope. I want the views to be loosely coupled to their parent window and not have to something like $scope.$parent

module.directive('window', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'windowTemplate.html',
        controller: 'windowController',
        scope: {
            config: '='
        },
        link: function(scope, element, attrs) {

        }
    };
});

module.directive('view', function($compile, $http) {
    return {
        restrict: 'E',
        replace: true,
        scope: {},
        link: function(scope, element, attrs) { 
            attrs.$observe('templateUrl', function (url) {
                $http.get(url).then(function (response) {
                    var tpl = $compile(response.data)(scope);
                    element.append(tpl);
                });
            });
        }
    };
});

我最初以为我可以与服务实现这一点,但在于服务是单身,视图将更新所有窗口。

I initially thought that I could achieve this with a service, but being that services are singletons, the view would update all windows.

有没有办法达到我想要做什么?

Is there a way to achieve what I'm trying to do?

Plunker

推荐答案

当我试图在<一个证明href=\"http://stackoverflow.com/questions/23964571/is-there-a-way-for-a-child-controller-to-inherit-a-service-like-instance-from/23964845#comment36991912_23964845\">this ,回答尽管服务是单身,有简单的方法来利用它们来管理多个独立的对象实例。

As I attempted to demonstrate in this answer, despite the fact that services are singletons, there are easy ways to utilize them for managing multiple, unique object instances.

我认为你摔跤真正的问题是如何与一个窗口和查看。这听起来像你有一个厌恶利用原型继承怕太元素紧密耦合。尽管如此,这种或那种方式,你将必须有视图和窗口之间的一些耦合。否则,他们是没有关系只是独立的对象到另一个。

I think that the real problem you're wrestling with is how to relate a "window" and a "view". It sounds like you have an aversion to utilizing prototypical inheritance for fear of too tightly coupling the elements. Still, one way or another, you're going to have to have some coupling between view and window. Otherwise, they are just independent objects with no relationship to one another.

我建议你考虑创建一个服务,这是,部分窗口和视图对象的工厂(即类似于我回答你的previous问题)。随后,由于使用的是具有隔离范围的指令,你可以分享的只是窗口的有孩子的意见,像这样(在窗口指令模板)范围对象:

I suggest that you consider creating a service which is, in part, a factory of window and view objects (ie. similar to my answer to your previous question). Then, since you are using directives with isolate scope, you can share just the window scope object with child views, like so (in your window directive template):

<view template-url="{{view.templateUrl}}" window="window" ng-show="view.active"></view>

如果您做出相应的范围:{...} 更改所有子视图的指令,他们将有机会获得的只有的父窗范围的变量。和诚实,在这种情况下,你甚至可以跳过制作服务,因为窗口并查看指令的能充分自行封装。无论你是否需要该服务可能会依赖于其它因素的你已经psented讨论$ P $的范围之外。

If you make the corresponding scope: {...} change to all child view directives, they will have access only to the parent's scope variable for window. And honestly, in that case, you might even skip making the service, since the window and view directive's could themselves sufficiently encapsulate. Whether or not you need the service will probably depend on other factors outside of the scope of what you've presented for discussion.

如果您看到共享只作用域变量的问题,你能对你的反对意见是什么更新的详细信息您的问题?

If you see an issue with sharing only that scope variable, can you update your question with more information on what your objections are?

这篇关于是否有更新从一个动态创建的视图/控制器父指示一个松耦合方式,是父母的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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