How would one run two controllers at the same time AngularJS -
i'm making webapp in angularjs (using yeoman), , have loop add 1 counter every second. worked fine, until needed multiple tabs multiple controllers. tried running loop in main controller, didn't work.
i want gameloop function in moneyctrl run @ times, there better place put function?
does have idea of how this, or @ least achieve same effect?
you can put gameloop function on run block.
angular.module('incrementalapp', ['nganimate', 'ngcookies', 'ngresource', 'ngroute', 'ngsanitize', 'ngtouch' ]).config(function($routeprovider) { //... }).run(function($rootscope, moneyservice, vars){ var stop; $rootscope.loop = function() { if (angular.isdefined(stop) ) { return; } stop = $interval(function() { angular.foreach(vars.clickers, function(clicker){ moneyservice.addmoney((clicker.amount * clicker.production)/vars.fps); }); }, 1000/vars.fps); $rootscope.stoploop = function() { if (angular.isdefined(stop)) { $interval.cancel(stop); stop = undefined; } }; $rootscope.$on('$destroy', function() { // make sure interval destroyed $scope.stopfight(); }); }; $scope.loop(); })
and use service add money can require service on each controller.
Comments
Post a Comment