AngularJS - 哪一个更好,发出$ / $或范围继承? [英] AngularJS - Which is better, $emit/$on or scope inheritance?

查看:109
本文介绍了AngularJS - 哪一个更好,发出$ / $或范围继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我得到了这个以下HTML结构:

Say I got this following HTML structure:

<body ng-app="demo" ng-controller="RootCtrl">
    <header>
        <!-- Header Material -->
    </header>

    <main ng-controller="MainCtrl">
        <!-- Main Content -->

        <nav ng-controller="NavCtrl">
            <!-- Navbar -->
        </nav>
    </main>
<body>

现在,假设 NavCtrl 需要处理这种情况下, RootCtrl 存在一种模式的范围 - 下条件将 $发出关于 / $更适合?和在什么条件下会是更好的通过继承范围内直接操作模式?

Now, suppose NavCtrl needs to manipulate a model that happens to exist under RootCtrl's scope - under which conditions would $emit/$on be better suited? And under which conditions would it be better to directly manipulate the model via scope inheritance?

推荐答案

如果你使用原型继承,你需要小心,因为它很容易使用在父母子女控制器相同的变量名时做出的错误。这可以通过确保$范围变量总是'有一个点在某处他们是可以避免的,但需要纪律,以确保你总是这样做。你也可以使用 $范围。$父。$​​父结构访问NavCtrl集中的变量在RootCtrl,但这是脆,基本上是捆绑你的控制器到DOM结构是向下行问题的良方。

If you're using prototypical inheritance, you need to be careful as it's easy to make errors when using same variable names in parent and child controllers. This can be avoided by making sure $scope variables always 'have a dot' in them somewhere, but needs discipline to make sure you always do this. You could also access a variable in NavCtrl set in RootCtrl using the $scope.$parent.$parent structure, but this is brittle and essentially ties your controllers to the DOM structure which is a recipe for problems down the line.

$排放/ $上有潜在的默默失败的问题,如果您在事件的名称的拼写错误,而且可以使它很难跟踪发生的事情在发生错误的情况。这是更好地节制地使用它们。 <一href=\"http://eburley.github.io/2013/01/31/angularjs-watch-pub-sub-best-practices.html\">http://eburley.github.io/2013/01/31/angularjs-watch-pub-sub-best-practices.html说,只有使用它们,当你需要让多个用户了解一个事件,这些用户需要做多辐射信息,以他们的观点。

$emit/$on have the issue of potentially silently failing if you make a typo in the name of the event, and can make it hard to follow what's happening in the event of an error. It's better to use them sparingly. http://eburley.github.io/2013/01/31/angularjs-watch-pub-sub-best-practices.html says only to use them "when you need to let multiple subscribers know about an event and those subscribers need to do more than radiate information to their view."

跨控制器共享数据模型的正常角度的方法是做一个服务,并注入到这两个控制器来代替。这符合与一般的太OOP原则 preFER在继承组成。

The normal Angular way to share data models across controllers is to make a service, and inject that into both controllers instead. This fits with the "prefer composition over inheritance" principle of OOP in general too.

app.service('dayService', function () {      
    var day = 'Monday';
    return {
        getDay: function() {
           return day;
        },
        setDay: function(thisDay) {
           day = thisDay;
        }
    };
})

function NavCtrl($scope, dayService) {
    $scope.day = dayService.getDay();
}

function RootCtrl($scope, dayService) {
    dayService.setDay('Sunday');
}

HTML

<nav ng-controller="NavCtrl">
    Today is {{day}}
</nav>

您也可以找到关于角最佳实践MISKO的影片有趣,它谈到摆在控制器VS什么周围的28分钟大关的服务,更多的事件($排放上/ $)接近尾声。他的结论(改写)是事件是有点问题最好只为两件事情真不需要知道彼此,并有情况下使用,以保持非常分开的,或者如果事件不总是需要有时可以忽略不计。

You might also find Misko's video on Angular best practices interesting, it talks about what to put in controllers vs services around the 28 min mark, and more about events ($emit/$on) towards the end. His conclusion (paraphrased) is that events are somewhat problematic best used only for situations where two things really don't need to know about each other and have to be kept very separate, or if the event is not always necessary can sometimes be ignored.

我想说的基本规则是:


  • 两个控制器之间的数据共享使用的服务,这是略多于继承,但没有更复杂太难了。

  • Use service for sharing data between two controllers, it's slightly more complex than inheritance but nothing too difficult.

使用事件以复杂的方式多个不同的用户之间共享。

Use events for sharing between multiple different subscribers in complex ways.

在控制器$范围应该是'只写'(直接从MISKO的最佳实践的视频拍摄规则的上方)。其中NavCtrl需要处理这种情况下RootCtrl的范围存在典范继承范围将涉及读父范围也一样,所以我觉得最好避免使用。

$scope in controllers should be 'write only' (rule taken directly from Misko's best practices video above). Scope inheritance where "NavCtrl needs to manipulate a model that happens to exist under RootCtrl's scope" would involve reading parent scope too, so I think is best avoided.

这篇关于AngularJS - 哪一个更好,发出$ / $或范围继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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