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);
}}注意如何select添加方法$scope,但是addPane添加了方法this。如果我將其更改為$scope.addPane,則代碼會中斷。文檔說實際上存在差異,但沒有提到差異是什么:以前版本的Angular(pre 1.0 RC)允許您this與該$scope方法互換使用,但現在不再是這種情況了。內的方法上的范圍限定this并且$scope是可互換的(角套this到$scope),但是不另外你的控制器構造內部。如何this和$scope在AngularJS控制器的工作?
AngularJS控制器中的'this'與$ scope
智慧大石
2019-05-29 14:41:29