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

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

问题描述

我在 AngularJS 风格指南

中找到了这个<块引用>

更喜欢使用控制器作为语法并使用变量捕获它:

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

所以,考虑到引用的建议,如果我对访问 $rootScope 中的任何内容不感兴趣,我什么时候应该使用 $scope ?

也就是说,我是否应该将所有内容都移动到控制器的this?如果不是,那么 $scope 中应该保留什么?

解决方案

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

  • $scope 不仅仅用于访问 $rootScope 中的属性或函数.示例(顺便说一下,不是太频繁): 假设您需要以非角度方式更新 DOM 元素,这意味着通过任何修改组件值(视觉上)的外部库更新它,但是组件的 ng-model 没有更新,你需要它!该怎么办?简单:$scope.$digest(取决于你做什么,可能需要任何其他角度函数).
<块引用>

我应该把所有东西都移到控制器的这个吗?

  • 不,你没有!实际上,这样做并不是一个好主意.为什么?当您将所有内容移动到控制器的 this 时,您就可以从视图中访问所有内容",这是不切实际的,因为您不需要在视图中将所有内容都在控制器中声明.在控制器中,有时(大部分时间)您使用变量和函数作为其他函数和变量(私有的东西)的补充:它们应该在控制器内部保持私有".请参见下面的示例:
<小时>

角度.module('myapp', []).controller('foo', function() {var vm = 这个;//由于控制器语法,只有这个在视图中可用vm.wizard = {用户 ID:空,总购物车计数:0}//只在控制器内部使用的东西vm.private = {小计ByProducts:[],准备结账}//这里只返回公共"的东西返回 vm.wizard;//职能});

<块引用>

如果不是,那么 $scope 中应该保留什么?

您在 $scope 中放入的内容完全取决于您,因为它使用 controller as 语法,但请记住 中的所有内容$scope 可在视图中访问.这个想法是减少传递到视图的变量数量.这在小型 web 应用程序中可能不明显,但是当应用程序的大小变大时,您会注意到一些变化(加载时间更长等).

这取决于每个开发者的观点以及我们每个人对使用最佳实践的喜好程度.

I found this in AngularJS style guide

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

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.

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?

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

解决方案

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

  • $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?

  • 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
  });

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

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