caching - AngularJS: route resolve promise is cached -
in first sample code works should - on every page route change check conditions ($timeout
) , resolve promise:
in second sample move var rulesfactory
app.factory('rulesfactory')
, works once:
jsfiddle app factory sample code
is cache? how solve improve code reusabilty?
the code in rulesfactory
ever running once - first time service instantiated.
it on resolve first time:
- angular sees it's looking inject called 'rulesfactory'
- angular finds service called rulesfactory. sees no 1 has used rulesfactory yet, , instantiates service.
- the service rulesfactory instantiated, , $timeout run promise returned
it on resolve second time:
- angular sees it's looking 'rulesfactory'. rulesfactory exists!
- angular gets existing object rulesfactory.
- the timeout has run out earlier, , same resolved promise used.
the fix have rulesfactory return function run timeout again - every time want it.
and additionally, $timeout returns promise, can return $timeout :-)
Comments
Post a Comment