I would like to reset the state of my angular app without forcing a page refresh. What's the idiomatic way to do this?
I don't know if there's an idiomatic way, but if you use manual bootstrap (ie, get rid of ng-app), then to reset you could
Example HTML:
<div id="myid" ng-controller="MyCtrl">
<button ng-click="i = i+1">Add 1</button>
<span>i = {{i}}</span>
<button ng-click="reset()">RESET</button>
</div>
Example controller/JS (with jQuery included as well), which would be run on onload:
var $cleanCopy = $("#myid").clone();
function bootstrap() {
angular.bootstrap(document.getElementById('myid'), ['mymodule']);
}
angular.module('mymodule', []).controller('MyCtrl', function($scope) {
$scope.i = 1;
$scope.reset = function() {
// You need this second clone or angular bootstraps
// into the original clone!
$("#myid").replaceWith($cleanCopy.clone());
bootstrap();
};
});
bootstrap();
Here is a working JSFiddle: http://jsfiddle.net/zd377/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With