In the first sample code all works as should - on every page route change we check some conditions ( $timeout
) and resolve a promise:
In the second sample we move var rulesFactory
to app.factory('rulesFactory')
and now it works once:
JSFiddle App Factory sample code
Is it cache? How to solve this to improve code reusabilty?
The code in your rulesFactory
was only ever running once - the first time the service itself was instantiated.
It does this on resolve the first time:
It does this on resolve the second time:
The fix for this is to have the rulesFactory return a function which will run the timeout again - every time you want it.
And additionally, $timeout itself returns a promise, so you can just return a $timeout :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.