2014-10-20 22 views
0

我有一个使用ng-repeat指令在HTML页面中显示的对象数组。每个项目点击后打开一个新的HTML页面。每当我点击任何项目时,都会将我带到新页面,当我点击时,它会将我重新定位到页面顶部(在第一项上)。在ng-repeat中保留元素的位置

什么是保留物品位置的最佳方式,例如当用户点击物品时,进入新页面并点击回来,然后他/她被带到触发该动作的相同元素。

演示将是可取的。

谢谢!

+0

什么是你的例子中的“页面”?在Angular中是“视图”吗?它是不同的端点网址吗?另外,如果“演示更好”,我会建议为你解决问题,并尽量减少其他人帮助你的工作。 – 2014-10-20 20:24:25

回答

0

如果你要离开你的页面,我想你有两种处理方式。 第一个可能是在离开页面之前强制页面到达ID的位置参照物。

即:click - > location.href('#myItemId') - > location.href('externalResource')。 http://jsfiddle.net/romulocollopy/gj2rss1v/6/

angular.module('myapp',[]) 
.controller("myCtrl",['$scope',function($scope){ 

    $scope.fruits = ['banana','aple','passion fruit', 'watermelon', 'spam','eggs']; 

    $scope.link_to_fruit_detail = function(fruit){ 
     location.href = '#/fruit'; 
     location.href = 'http://www.'+fruit+'.com'; 
    } 
}]); 

另一种方法是点击的y位置存储在cookie: http://jsfiddle.net/romulocollopy/gj2rss1v/7/

angular.module('myapp',['ngCookies']) 
.controller("myCtrl",['$scope', '$cookieStore',function($scope, $cookieStore){ 
    if ($cookieStore.get('y-click')){ 
     var y = $cookieStore.get('y-click') 
     window.scrollTo(y,0); 
     console.log(y) 
    } 

    $scope.fruits = ['banana','aple','passion fruit', 'watermelon', 'spam','eggs']; 

    $scope.link_to_fruit_detail = function($event, fruit){ 
     location.href = 'http://www'+fruit+'.com'; 
     $cookieStore.put('y-click', $event.y); 
    } 
}]); 

展位有有利点和缺点,但我想不出更好的办法。