gpt4 book ai didi

javascript - 从 AngularJS 中的另一个页面链接到 #ID 元素

转载 作者:行者123 更新时间:2023-11-30 05:32:59 24 4
gpt4 key购买 nike

我已经尝试了这里提供的所有解决方案: How to handle anchor hash linking in AngularJS

但没有一个对我有用。

在我的 header.html我有一个链接:<a id="button" href="#/views/home#page"> Contact</a></li>

home.html 中的 ID

当我在 /home它有效,但是当我在另一个页面时它不起作用。

我尝试使用 ##page没有成功。或者把它放在 app.js 中:

app.run(function($rootScope, $location, $anchorScroll, $routeParams) {
$rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
$location.hash($routeParams.scrollTo);
$anchorScroll();
});
});

并自定义我的链接:

href="#/views/home/?scrollTo=page"

有人可以解释我应该编辑哪些文件以及如何编辑吗?

编辑:我从Angular-Seed开始

我的 app.js是:

'use strict';

// Declare app level module which depends on views, and components
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.training',
'myApp.faq',
'myApp.media',
'myApp.contact',
'myApp.home',
'myApp.apply',
'myApp.classes',
'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.otherwise({redirectTo: '/views/home'});
}]);

在每个 View 中我都有另一个 js 文件,如 training.js看起来像:

'use strict';

angular.module('myApp.training', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/views/training', {
templateUrl: 'views/training/training.html',
controller: 'TrainingCtrl'
});
}])

.controller('TrainingCtrl', [function() {

}]);

基于angular-seed模型配置。

所以,什么时候,我在视野中/faq有部分 header使用菜单和所有链接,我如何链接到 View 中的特定 ID /home

最佳答案

我认为问题在于您有逻辑滚动到 $routeChangeSuccess 中的那个散列,并且作为 documentation says, this event is :

Broadcasted after a route dependencies are resolved. ngView listens for the directive to instantiate the controller and render the view.

因此 View 尚未呈现,因此具有该 id 的 DOM 元素尚不存在。

尝试将该逻辑放入 ngView directiveonload 事件中相反。

我创建了这个插件:http://plnkr.co/edit/S7bUT8iYY7UEti71X5Z8?p=preview这表明如果您将该逻辑添加到 ngView directiveonload 事件中一切正常。

关于javascript - 从 AngularJS 中的另一个页面链接到 #ID 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25716936/

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