我应该使用`this`或`$ scope`吗? [英] Should I use `this` or `$scope`?

查看:68
本文介绍了我应该使用`this`或`$ scope`吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种用于访问控制器功能的模式:this$scope.

There are two patterns in use for accessing controller functions: this and $scope.

我应该在什么时候使用?我知道this被设置为控制器,并且$scope是视图范围链中的一个对象.但是使用新的"Controller as Var"语法,您可以轻松使用其中任何一个.所以我要问的是什么是最好的,未来的方向是什么?

Which should I use and when? I understand this is set to the controller and $scope is an object in the scope chain for views. But with the new "Controller as Var" syntax, you can easily use either. So what I'm asking is what is best and what is the direction for the future?

示例:

  1. 使用this

function UserCtrl() {
  this.bye = function() { alert('....'); };
}

<body ng-controller='UserCtrl as uCtrl'>
  <button ng-click='uCtrl.bye()'>bye</button>

  • 使用$scope

    function UserCtrl($scope) {
        $scope.bye = function () { alert('....'); };
    }
    

    <body ng-controller='UserCtrl'>
        <button ng-click='bye()'>bye</button>
    

  • 与其他Java OO模式相比,我个人认为this.name在视觉上更轻松,更自然.

    I personally find the this.name to be easier on the eye and more natural compared to other Javascript OO patterns.

    请咨询?

    推荐答案

    两者都有其用途. 首先,是一些历史记录...

    $ scope是经典"技术,而"controller as"则是最近的技术(从1.2.0版开始正式发行,尽管在此之前它确实出现在不稳定的预发行版中).

    $scope is the "classic" technique while "controller as" is much more recent (as of version 1.2.0 officially though it did appear in unstable pre-releases prior to this).

    两者都能很好地工作,唯一的错误答案是在没有明确原因的情况下将它们混合在同一应用中.坦白说,将它们混合会起作用,但只会增加混乱.因此,选择一个并滚动.最重要的是要保持一致.

    Both work perfectly well and the only wrong answer is to mix them in the same app without an explicit reason. Frankly, mixing them will work, but it will just add to the confusion. So pick one and roll with it. The most important thing is to be consistent.

    哪个?那取决于你. $ scope中还有更多示例,但是"controller as"也在不断发展.这个比那个好吗?这值得商..那你怎么选择呢?

    Which one? That depends on you. There are many more examples out there of $scope, but "controller as" is picking up steam as well. Is one better than the other? That's debatable. So how do you choose?

    舒适

    我更喜欢"controller as",因为我喜欢隐藏$ scope并通过中间对象将成员从控制器暴露给视图.通过设置this.*,我可以将要从控制器公开的内容公开给视图.您也可以使用$ scope做到这一点,我只喜欢为此使用标准JavaScript.实际上,我是这样编写的:

    I prefer the "controller as" because I like hiding the $scope and exposing the members from the controller to the view via an intermediary object. By setting this.*, I can expose just what I want to expose from the controller to the view. You can do that with $scope too, I just prefer to use standard JavaScript for this. In fact, I code it like this:

    var vm = this;
    
    vm.title = 'some title';
    vm.saveData = function(){ ... } ;
    
    return vm;
    

    这对我来说更干净,并且可以轻松查看视图中所暴露的内容.注意,我将返回的变量命名为"vm",它代表viewmodel.那只是我的约定.

    This feels cleaner to me and makes it easy to see what is being exposed to the view. Notice I name the variable that I return "vm" , which stands for viewmodel. That's just my convention.

    使用$ scope,我可以做同样的事情,因此,我不会增加或减少这项技术.

    With $scope I can do the same things, so I'm not adding or detracting with the technique.

    $scope.title = 'some title';
    $scope.saveData = function() { ... };
    

    所以这取决于你.

    注射

    使用$ scope,我确实需要将$ scope注入到控制器中.我不必对控制器执行此操作,除非出于其他原因(例如$ broadcast或watchs,尽管我尝试避免在控制器中监视),否则我也不需要这样做.

    With $scope I do need to inject $scope into the controller. I don't have to do this with controller as, unless I need it for some other reason (like $broadcast or watches, though I try to avoid watches in the controller).

    更新 我写了这篇关于这两种选择的文章: http://www.johnpapa.净/您喜欢带糖或不带糖的角度控制器/

    UPDATE I wrote this post about the 2 choices: http://www.johnpapa.net/do-you-like-your-angular-controllers-with-or-without-sugar/

    这篇关于我应该使用`this`或`$ scope`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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