相关疑难解决方法(0)

AngularJS控制器中的'this'与$ scope

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

注意如何select添加方法$scope,但是addPane添加了方法this.如果我将其更改为$scope.addPane,则代码会中断.

文档说实际上存在差异,但没有提到差异是什么:

以前版本的Angular(pre 1.0 RC)允许您this与该$scope方法互换使用,但现在不再是这种情况了.内的方法上的范围限定this并且$scope是可互换的(角套this$scope),但是不另外你的控制器构造内部.

如何this$scope在AngularJS控制器的工作?

this angularjs angularjs-scope

1013
推荐指数
5
解决办法
29万
查看次数

标签 统计

angularjs ×1

angularjs-scope ×1

this ×1