Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to change some of the variables inside my angular modules from the console and keep hitting roadblocks.

I have a particular property that will eventually be provided by the backend server. Until that part is ready, I would like to test my templates by changing the property from the console.

So if I borrow an example from the angularjs doc:

myApp.value('clientId', 'a12345654321x');
myApp.controller('DemoController', ['$scope', 'clientId', function DemoController($scope, clientId) {
  $scope.clientId = clientId;
}]);

I would like to change the value at runtime from the console. Does anyone know of a way how?

I tried using a different global object but the digest is not picking up changes

var myNamespace = {clientId: "a12345654321x"};

myApp.controller('DemoController', ['$scope', function DemoController($scope) {
  $scope.specialGlobal = myNamespace;
}]);

//then in html template
<h1>{{specialGlobal.clientId}}</h1>

This works on initial load, but changes to myNamespace.clientId through the console are not propagated.

Runnuning angularjs 1.3.x

share|improve this question
    
you can use text box for display and change of value, that what i do for testing –  HarishR 18 hours ago

1 Answer 1

up vote 4 down vote accepted

You can get the current scope in the console via angular.element - target an ID that is within your controller:

var scope = angular.element(document.querySelector("#idOfElementInController")).scope();

Now you can change variables on the $scope via scope

scope.specialGlobal = "new value";
share|improve this answer
1  
What he said... but note that you may also need to trigger an $apply() cycle for Angular to see your new value... Can't wait for ES6 Observers to fix this... –  Chad Robinson 18 hours ago
    
Perfect!! I did have to scope.$apply() –  mastaBlasta 18 hours ago
    
If you are using Chrome, you could use $0 for the current selected element in the Element panel of DevTools. –  runTarm 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.