Ionicframework 中 $window 和 window 的区别

Difference between $window and window in Ionicframework

ionic-framework 中 $window 和 window 的确切区别是什么?

例如,在 learn.ionicframework.com/formulas/localstorage/ 的 localstorage 教程中,同时使用了 $window 和 window。

$window 是一个 Angular 包装全局变量 window 的服务,主要是为了可以模拟它进行单元测试:

A reference to the browser's window object. While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In angular we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.

如果你看at the source,你会发现后面没有更多内容:

function $WindowProvider() {
  this.$get = valueFn(window);
}

两者都与离子框架无关。

window 是一个浏览器对象,表示包含 DOM 的 window。 我们用于 javascript 的 'document' 是 属性 的 window 对象 window.document.

参考这个 browser window object

其中 $window 是在 angular 中创建的服务,可以再次注入到在 angular 中编写的任何服务或控制器以使用其 API。

参考这个 $window

$window