'this' vs $ scope в контроллерах AngularJS

в& quot; Создать компоненты & quot; раздел домашней страницы AngularJSВот этот пример:

controller: function($scope, $element) {
  var panes = $scope.panes = [];
  $scope.select = function(pane) {
    angular.forEach(panes, function(pane) {
      pane.selected = false;
    });
    pane.selected = true;
  }
  this.addPane = function(pane) {
    if (panes.length == 0) $scope.select(pane);
    panes.push(pane);
  }
}

Обратите внимание, какselect метод добавлен в$scope, ноaddPane метод добавлен вthis, Если я изменю это на$scope.addPaneкод нарушается.

Документация говорит, что на самом деле есть разница, но она не упоминает, в чем разница:

Previous versions of Angular (pre 1.0 RC) allowed you to use this interchangeably with the $scope method, but this is no longer the case. Inside of methods defined on the scope this and $scope are interchangeable (angular sets this to $scope), but not otherwise inside your controller constructor.

Какthis а также$scope работать в контроллерах AngularJS?

Ответы на вопрос(7)

Ваш ответ на вопрос