gpt4 book ai didi

javascript - 在浏览器中更改 URL 参数时进行 Angular 页面刷新

转载 作者:行者123 更新时间:2023-12-02 16:10:05 26 4
gpt4 key购买 nike

非常简单的要求,但很难做到这一点。有一个简单的页面,我在其中打印 url 路径中的参数。

http://localhost:8888/ngtest.html#/1234567

ngtest.html

<div ng-app="app" ng-controller="testCtrl">
{{myId}}
</div>

<script>
var app = angular.module('app', []);
app.controller('testCtrl', function($scope, $location) {
$scope.myId = $location.$$path;
});
</script>

在浏览器窗口中:

  1. 1234567 更改为 1234
  2. 按 Enter

什么也没发生。按 F5,它会按预期工作。

不确定这是否与浏览器行为有关,但如何在参数更改 + Enter 后强制刷新页面?

最佳答案

根据上面 Leon 建议的完整示例

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>

<div ng-app="app" ng-controller="testCtrl">
{{myId}}
</div>

<script>
var app = angular.module('app', []);
app.controller('testCtrl', function($scope, $location) {

// add a listener for when the url changes and user
// Enter in the address bar
$scope.$on('$locationChangeStart', function(event) {
$scope.myId = $location.$$path;
});
});
</script>

基本上,位置这是注册一个位置更改监听器并相应地执行操作。无需路由配置

关于javascript - 在浏览器中更改 URL 参数时进行 Angular 页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com