2

次のコードがあります。

HTML

<div ng-controller="MyCtrl">
   {{message}}!
    <button ng-click="changeMessage()">change Message</button>
</div>

AngularJs

var myApp = angular.module('myApp',[]);

function MyCtrl($scope, $http) {
    $scope.message = 'Hello $scope';
    $scope.newMessage = 'Hello World';
    $scope.changeMessage = function(){
        $scope.message = $scope.newMessage;//debugger;
    }
}

このシナリオでは、問題はありません。ただし、この行を変更すると、次のようになります。

$scope.message = $scope.newMessage;

と:

debugger;

次に、コンソールが開いている間にボタンをクリックして と書く$scopeと、次のエラーが表示されます。

Uncaught ReferenceError: $scope is not defined

だから私の質問は:

  • 関数内で$scopeを使用すると にアクセスでき、デバッグ モードではアクセスできない理由(関数のどこかで を使用していない場合)$scope$scope
  • あまりにもアクセスできない$httpため、この状況での作業方法$http

Jsfiddle

注:これは重複した質問かもしれませんが、この問題の直接的な説明と解決策は見つかりませんでした

4

0 に答える 0