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?
3 Answers
You may access a controller's scope in the console by:
- Select the node in the elements tab by clicking on it.
- 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');
Comments
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
opengl
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
Mikalai
$scope.$apply(function () { $scope.score = 1; }); This should change value of score to 1.
opengl
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/79bv55Mikalai
$scope = $(#game-result-score-value).scope(); You must initialize $scope before using it.
opengl
Thank you!! I just started angular :)