【问题标题】:I cannot use history.pushState while using angular's $location service使用 Angular 的 $location 服务时我不能使用 history.pushState
【发布时间】:2017-05-09 04:05:08
【问题描述】:

https://jsfiddle.net/leathan/8bstxhc6/4/

基本上在上面的 jsfiddle 中,如果你从上面的代码中删除 $location 然后运行它并在 7 秒前单击链接,你会收到“Did_it_work”的警报。如果您不删除$location,那么当您单击链接时它会中断。

编辑: 如果我使用标签 ng-include ...

,pushState 也会中断

【问题讨论】:

  • 以上代码中的$location是什么?
  • 在新链接中修复了它
  • 为避免与AngularJS框架冲突,请避免直接使用pushState。而是使用AngularJS $location.state() method 推送状态。 $location 服务与 AngularJS 框架及其摘要循环集成。有关详细信息,请参阅AngularJS Developer Guide - Using $location
  • 我可以举个例子吗?我不确定如何使用 $location.state()
  • state([state]); 但是我尝试了$location.state("","","test") 并没有用?

标签: javascript angularjs ecmascript-6 ngroute


【解决方案1】:

正如@georgaewg 指出的 AngularJS 摘要循环导致我的代码中断。

$location.path() 实际上已经足够满足我的需要了。

$location.state({foo:'bar'}).path('path_here') 例如,如果我想存储 $location.state().foo.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多