AngularJS:如果历史记录为空,则禁用 html 按钮

AngularJS: Disable html button if history is empty

在我的 AngularJS 应用程序中,我实现了一个具有 "go back" 功能的 html 按钮。如果历史是空的(因为用户在页面加载后没有更改路径)我想 hide/disable 它。

有没有办法用angular检测历史是否为空?

你甚至可以检查 angular js 的 $window 服务。它与 javascript 的 window 对象完全相同。

The history object contains the URLs visited by the user (within a browser window).

The history object is part of the window object and is accessed through the window.history property.

如果你想查看 history 是否为空,你应该使用

if( $window.history.length <= 1 ) {
  //Internet Explorer and Opera start at 0, while Firefox, Chrome, and Safari start at 1
  console.log("empty");
}

请确保您已在控制器中注入 $window 服务。 history 对象的一些函数是 here. You can write more lines to detect browser first and after then apply your conditions in angular way. Please check this post