角:我应该用这个或$范围 [英] Angular: Should I use this or $scope

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

问题描述

有使用两种模式来访问控制器的功能:本和$范围。
我应该使用什么时候?我明白这个设置为控制器和$范围是您欣赏到作用域链的对象。但是,随着新的控制器VAR语法,你可以很容易要么使用。那么,我问的是什么是最好的,什么是未来的方向是什么?

There are two patterns in use for accessing controller functions: "this" and "$scope". 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. 使用本

  1. Using "this":

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

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


  • 使用$范围

  • Using $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 easier on the eye and more natural compared to other Javascript OO patterns.

    高见?

    推荐答案

    两者都有自己的用途。 首先,一些历史...

    Both have their uses. First, some history ...

    $范围为经典技术,而控制器是更近(为1.2.0版本正式虽然它没有出现之前,这种不稳定的pre-版本)。

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

    哪一个?这取决于你。还有更多的例子在那里$范围,但控制器正在加速为好。是其中一个比另一个更好吗?这是值得商榷的。那么,你如何选择?

    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?

    舒适

    我preFER的控制器,因为我喜欢隐藏$范围,并通过一个中间对象暴露从控制器到视图中的成员。通过设置这个*,我可以暴露我想从控制器到视图揭露什么。你可以做到这一点与$范围过于,我只是preFER使用标准的JavaScript这一点。事实上,我code这样的:

    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.

    使用$范围,我可以做同样的事情,所以我不添加或使用该技术减损。

    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() { ... };
    

    因此​​,它取决于你那里。

    So its up to you there.

    使用$范围我需要注入$范围到控制器。我没有与控制器要做到这一点,除非我需要它的一些其他原因(如$广播或手表,但我会尽量避免在控制器的手表)。

    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种选择:
    <一href=\"http://www.johnpapa.net/do-you-like-your-angular-controllers-with-or-without-sugar/\">http://www.johnpapa.net/do-you-like-your-angular-controllers-with-or-without-sugar/

    这篇关于角:我应该用这个或$范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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