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

查看:37
本文介绍了我应该使用`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>
    

  • 我个人认为 this.name 与其他 Javascript OO 模式相比更容易看并且更自然.

    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" ,它代表视图模型.那只是我的约定.

    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 或手表,但我尽量避免在控制器中使用手表).

    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).

    更新我写了这篇关于 2 个选择的文章:http://www.johnpapa.net/do-you-like-your-angular-controllers-with-or-without-sugar/

    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天全站免登陆