在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控制器的工作?