4

I'm learning Angular and I made a simple game, I'm using ng-inspector to view current variable value, but how can I cahange variable value with browser console?

1
  • you could take use of debugger & put it in controller..then play with scope in console.. Commented May 25, 2015 at 19:35

3 Answers 3

7

You may access a controller's scope in the console by:

  1. Select the node in the elements tab by clicking on it.
  2. Execute var scope = angular.element($0).scope();

$0 is a reference to the selected DOM element in the console (webkit).

If instead you want access to factory/service variables you may use the injector method:

var injector = angular.element($0).injector();

var srv = injector.get('serviceName');
Sign up to request clarification or add additional context in comments.

Comments

5

You can get $scope from jquery/jqlite selector like desribed here, see function scope(). Change model attached to $scope and call $apply on this scope.

So the full script will be:

var $scope = $(#game-result-score-value).scope();
$scope.$apply(function () { $scope.score = 1;  });

5 Comments

This works in some way, I get all variables like this, but I need value of only 1 variable. Here is the screenshot: prntscr.com/79bpu4
$scope.$apply(function () { $scope.score = 1; }); This should change value of score to 1.
I already tried that with $scope.score = 1; and now yours too, and I still get error "Uncaught ReferenceError: $scope is not defined" SS: prntscr.com/79bv55
$scope = $(#game-result-score-value).scope(); You must initialize $scope before using it.
Thank you!! I just started angular :)
0
  • Select the node in the elements tab by clicking on it.
  • $0 is a reference to the selected DOM element in the console (webkit).

    var scope = angular.element($0).scope(); console.log(scope); scope.score=1; scope.$apply(function () { }); assuming score is a scope variable

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.