AngularJS:$ scope vs this:$ scope的用途是什么? [英] AngularJS: $scope vs this: what is the use of $scope?

查看:116
本文介绍了AngularJS:$ scope vs this:$ scope的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AngularJS风格指南中找到了这个


首选使用控制器作为语法并使用变量捕获:

Prefer using controller as syntax and capture this using a variable:

意味着我可以通过它将所有功能和模型分配给控制器,并通过视图中的别名进行访问。如果我这样做,我发现我对 $ scope 不再有用了。一个例外是当我想要访问 $ rootScope 中的内容时。

That means that I can assign all my functions and the model to the controller via this and access via the alias in the view. If I do so, I find that I really have no use for the $scope anymore. One exceptions is when I want to access something that's in the $rootScope.

所以,引用的建议是记住,如果我不想访问 $ rootScope 中的任何内容,我应该何时使用 $ scope

So, having the quoted suggestion in mind, when should I use $scope at all if I'm not interested in accessing anything in the $rootScope?

也就是说,我应该把所有东西都移到控制器的这个吗?如果没有,那么应该留在 $ scope

That is, should I move everything to controller's this? If not, then what should stay in the $scope?

推荐答案


如果我对$ rootScope中的任何内容不感兴趣,我应该何时使用$ scope?

when should I use $scope at all if I'm not interested in accessing anything in the $rootScope?




  • $ scope 不只是用于访问 $ rootScope 中的属性或函数。 示例(不太经常,顺便说一句):假设您需要更新DOM元素而不是以角度方式更新,这意味着通过任何修改组件值的外部库(可视化)更新它,但是组件的 ng-model 没有得到更新,你需要它!该怎么办?简单: $ scope。$ digest (取决于你所做的,可能还需要任何其他角度函数)。

    • $scope is not just for accessing to properties or functions in the $rootScope. Example (not too often, by the way): Suppose you need to update a DOM element not in the angular way, it means updating it through any external library which modifies the component value (visually), but the ng-model of the component doesn't get updated and you need it to! What to do? Simple: $scope.$digest (depending on what you do, there might be any other angular function required).

    • 我应该把所有东西都搬到控制器吗?

      should I move everything to controller's this?




      • 不,你没有!实际上这样做并不是一个好主意。为什么?当您将所有内容移动到控制器的 this 时,您将从视图中提供对所有内容的访问,这是不切实际的,因为您在视图中不需要在控制器中声明所有内容。在控制器中有时(大多数情况下)你使用的变量和函数只是作为其他函数和变量(私有东西的种​​类)的补充:它应该在控制器内保持私有。见下面的例子:

        • No, you don't! Actually it is not a good idea to do that. Why? When you move everything to the controller's this, you are providing access to that "everything" from the view, which is impractical, since you don't need in the view everything is declared in the controller. In the controller sometimes (most of the time) you have variables and functions used just as complementary to others functions and variables (kind of private stuff): that should be kept "private", inside the controller. See example below:
        • angular
            .module('myapp', [])
            .controller('foo', function() {
          
              var vm = this;
              // only this will be available in the view thanks to the controller as syntax
              vm.wizard = {
                userID: null,
                totalCartCount: 0
              }
          
              // stuff used only inside the controller
              vm.private = {
                subtotalByProducts: [],
                readyToCheckoout
              }
          
              // only "public" stuff is returned here
              return vm.wizard;
          
              // functions
            });
          




          如果没有,那么应该留在$ scope?

          If not, then what should stay in the $scope?

          你在 $ scope 中的内容完全取决于你,因为它是使用控制器作为语法,但请记住,在视图中可以访问 $ scope 中的所有内容。我们的想法是减少传递到视图的变量。这在小型webapp中并不明显,但是当应用程序的大小变大时,您可以注意到一些更改(更多时间加载等)。

          What you put in your $scope is completely up to you, as it is using the controller as syntax, but keep in mind that everything in the $scope is accessible in the view. The idea is to reduce the amount of variables passed to the view. This could not be noticeable in small webapp, but when the size of the app gets greater you can notice some changes (more time loading, etc).

          这是一个问题每个开发人员的观点以及我们任何人使用最佳实践的喜好。

          This is a matter of perspective of each developer and how fond could be any of us of using best practices.

          这篇关于AngularJS:$ scope vs this:$ scope的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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