Changing AngularJS variable using console
I am learning Angular and I made a simple game, I use ng-inspector to view the current value of a variable, but how can I change the value of a variable using the browser console?
source to share
You can get $scope
from the selector jquery
/ jqlite
as described here , see function scope()
. Modify the model bound to $scope
and call $apply
in this area.
So the complete script would be:
var $scope = $(#game-result-score-value).scope();
$scope.$apply(function () { $scope.score = 1; });
source to share
You can access the controller scope in the console:
- Select the node tab in the items tab by clicking on it.
- Execute
var scope = angular.element($0).scope();
$0
- link to the selected DOM element in the console (webkit).
If you want to access factory / service variables instead , you can use the injector method:
var injector = angular.element($0).injector();
var srv = injector.get('serviceName');
source to share
- Select the node tab in the items tab by clicking on it.
- $ 0 is a link to the selected DOM element in the console (webkit).
var scope = angular.element ($ 0) .scope (); console.log (scale); scope.score = 1; scope. $ apply (function () {}); the score is assumed to be a scope variable
source to share