If you've been working with AngularJS you've no doubt run into JQlite and with it you'll be able to do things like addClass(), css(), etc. I spent some time creating an affix directive (makes an element fixed after a certain scroll position) and to do this I had to bind a scroll event on the window.
This is very basic version of my directive but because AngularJS is a single page application (SPA) it technically never changes documents so when you change routes you will still find that those binded events are still firing even though the directive scope has been trashed.
To remedy this you will have to listen to a route change and unbind the event as such. This code snippet assumes that you are using ngRoute but other routing libraries also have a similar signal.
If you want a live demo click here
If you comment out the 'unbind' event and switch from the first page to the second page you will notice that the console log messages are still firing because the event is still binded.
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
jqLite 是 AngularJS 為了方便進行 DOM 操作而又不希望讓 jQuery 成為 dependency 的解決方案,官方文件如是說:
jqLite is a tiny, API-compatible subset of jQuery that allows Angular to manipulate the DOM. jqLite implements only the most commonly needed functionality within a very small footprint, so only a subset of the jQuery API - methods, arguments and invocation styles - are supported.
並且在文件下方給了一串相關 API 文件的連結,連到 jQuery API Documentation 去。
嗯?連到 jQuery API Documentaion 有什麼不對嗎?
前言
爬一爬 jqLite 的 source code 就可以知道,其實它是嘗試照著 jQuery 的部分 API 做 subset-implementation,結果就是雖然它給了 jQuery API Documentation 的連結,它部分 API 的部分功能是與 jQuery 不同的。如果你還是照著使用 jQuery 的用法來使用 jqLite 而沒有做好爬 jqLite source code 的心理準備,應該會有點 happy ...