我使用AngularJS的locationhash()+ $ anchorScroll将选定的页面元素移动到窗口顶部,一旦其内容通过Ajax加载。
JS: 在控制器中:
$scope.scrollTo = function (location) {
//Scroll to category head
$scope.categoryHead = "grouptitle-" + location;
$location.hash($scope.categoryHead);
$anchorScroll($scope.categoryHead);
};
在指令中:
.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
var scroll;
if (scope.$last === true) {
$timeout(function () {
//Scroll category to top of page after list has completed render
scroll = scope.scrollTo(scope.category);
});
}
}
};
这给了我一个mysite.com/##grouptitle-2
或类似的显示网址,看起来有些神秘。有没有办法配置这个锚点,以便它只显示一个哈希值,或者根本不修改地址栏URL?
答案 0 :(得分:0)
删除参数。 anchorScroll将自动从$anchorScroll
获取哈希值。
所以你的功能应该是这样的:
$location.hash(<hash>)
如果DOM是动态更新的,那么将$scope.categoryHead = "grouptitle-" + location;
$location.hash($scope.categoryHead);
$anchorScroll();
和$location.hash
包裹在$anchorScroll
函数中。
答案 1 :(得分:0)
执行以下步骤: 在这个$ location.hash($ scope.categoryHead)之后; 添加
private void refreshConnections() {
// remove old connections
anchorPane.getChildren().removeAll(connections);
connections.clear();
if (stadte.size() >= 2) {
// connect selected cities
Iterator<Stadt> iter = stadte.iterator();
Stadt city = iter.next();
while (iter.hasNext()) {
Stadt nextCity = iter.next();
connect(city, nextCity);
city = nextCity;
}
if (stadte.size() >= 3) {
// connect to start, if there are more than 2 selected cities
connect(city, stadte.get(0));
}
}
}
@FXML
private handleSelectionChange(ActionEvent event) {
ToggleButton button = (ToggleButton) event.getSource();
Stadt city = (Stadt) button.getUserData();
if (button.isSelected()) {
stadte.add(city);
} else {
stadte.remove(city);
}
refreshConnections();
}
private void addCity(String name, RadioButton radio) {
Stadt city = new Stadt(name, radio.getLayoutX(), radio.getLayoutY(), radio.isSelected());
stadte.add(city);
radio.setUserData(city);
}
public void addCities(){
addCity("Zug", zug);
addCity("Zurich", zurich);
...
}
$ location.replace()函数将从网址中删除“#”。
答案 2 :(得分:0)
执行以下步骤: 在这个$ location.hash($ scope.categoryHead)之后; 添加
{{1}}
$ location.replace()函数将删除&#39;#&#39;来自网址。