AngularJS $ anchorScroll将## - anchorname添加到页面URL。我怎么能避免这个?

时间:2013-11-15 11:02:02

标签: javascript angularjs

我使用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?

3 个答案:

答案 0 :(得分:0)

<{1>}上的

删除参数。 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;来自网址。