Scenario
Say we have an input element in the container that we name TestCtrl, as follows<div id="TestCtrl" ng-controller="TestCtrl>
<input type="text" id="testInput" value="{{scopeTest}}" />
</div>
Problem
Now say if the value of the input element testInput changes outside the controller. With something like thisfunction nonControllerFunction(){
var valElem = document.getElementById("testInput");
valElem.value = "value changed"
}
Scope variables outside AngularJS controller
The solution is actually pretty straight forward, we make use of the awesome $apply method of the scope. So instead of the above approach we use the following functionfunction changeValue(){
var elem = document.getElementById("TestCtrl");
var valElem = document.getElementById("testInput");
var scope = angular.element(elem).scope();
scope.$apply(function(){
scope.scopeTest = 'Value changed';
});
}
References
If you find any of my posts useful and want to support me, you can buy me a coffee 🙂
https://www.buymeacoffee.com/bhumansoni
Or you can buying or even try one of my apps on the App Store.
https://mydaytodo.com/apps/
Also, if you can leave a review on the App Store or Google Play Store, that would help too.
0 Comments