search

Home  >  Q&A  >  body text

angular.js - angularjs的run方法中注入了$localStorage,在控制台打印为什么是undifined

我在登录的时候把用户名存在localStorage中,想在stateChangeStart中根据localStorage的信息验证权限,可是我在run方法中注入$localStorage中获取不到$localStorage的值,页面会报错,$localStorage为undifined,这是怎么回事?

世界只因有你世界只因有你2744 days ago585

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-15 17:13:05

    localStorage is a property under window, not angularJS. So there is no need to inject it, just use it directly

    if( window.localStorage ){
        //do sth...
    }
    

    For details, please refer to:

    https://developer.mozilla.org...

    reply
    0
  • Cancelreply