gpt4 book ai didi

javascript - 在 Controller 中滚动后获取 ng-repeat 的索引 - Angular 、 Ionic

转载 作者:行者123 更新时间:2023-12-03 08:37:49 26 4
gpt4 key购买 nike

下面的代码在浏览器中工作正常,但当我在移动设备中执行相同操作时,它不起作用,

这是我的代码笔链接,http://codepen.io/sudan_1993/pen/BowzbN

HTML 文件

<html ng-app="bumbleBee">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.3/angular.min.js"> </script>
</script>
<script src="http://airve.github.io/js/verge/verge.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"> </script>
</head>
<body>
<ion-view>
<ion-content overflow-scroll="true">
<div ng-controller="repeatCtrl">
<h1 ng-bind="title"></h1>
<!--if you want search in all data use searching.$ -->
<input type="text" placeholder="search All Data" ng-model="searching.$" /> OR
<!-- Searching by Name -->
<input type="text" placeholder="search by Name" ng-model="searching.name" /> OR
<!-- Searching by Age -->
<input type="text" placeholder="search by Age" ng-model="searching.age" />
<ul>
<li scroll="atNewArticle($index)" scroll-item="cat" ng-repeat="cat in cats | filter:searching">{{cat.name +" - " + cat.age+ " - "+ cat.gender}}</li>
</ul>
</div>
</ion-view>
</ion-content>
</body>
</html>

Controller :

var myApp = angular.module('bumbleBee', []);

myApp.directive('scroll', function ($parse, $document, $window) {
console.log("inside scroll");
var _ = $window._;
var verge = $window.verge;
var visibleElements = [];
console.log(JSON.stringify(verge) + '\n' + _ + '\n' + visibleElements[0]);

return {
restrict: 'A',
scope: {
scroll: '&',
scrollItem: '='
},
link: function (scope, element, attrs) {
console.log(_.debounce)
var debounced = _.debounce(function() {
// You might need a different test,
// perhaps including the height of the element,
// or using verge "rectangle" function
console.log("came inside link");
var visible = verge.inViewport(element);

var index = visibleElements.indexOf(scope.scrollItem);
var previouslyVisible = (index != -1);
if (visible && !previouslyVisible) {
visibleElements.push(scope.scrollItem);
scope.$apply(function() {
scope.scroll({item:scope.scrollItem});
});
}
if (!visible && previouslyVisible)
{
visibleElements.splice(index, 1);
}
}, 500);
angular.element($document).on('scroll', debounced);
if (verge.inViewport(element)) {
visibleElements.push(element);
}

}
};
});

myApp.controller('repeatCtrl', ['$scope', function($scope){

//Creating Angular Project Title
$scope.title = "Title";

//Initilal JSON Object
$scope.cats= [
{name:'John', age:25, gender:'boy'},
{name:'Jessie', age:30, gender:'girl'},
{name:'Johanna', age:28, gender:'girl'},
{name:'Joy', age:15, gender:'girl'},
{name:'Mary', age:28, gender:'girl'},
{name:'Peter', age:95, gender:'boy'},
{name:'Sebastian', age:50, gender:'boy'},
{name:'Erika', age:27, gender:'girl'},
{name:'Patrick', age:40, gender:'boy'},
{name:'Samantha', age:60, gender:'girl'}
];

$scope.atNewArticle = function(item) {
console.log(item);
}



}])

它不会进入指令中的链接内部。_.debounce 是否有任何问题?谁能帮忙解决这个问题吗?

最佳答案

不要使用 ng-repeat,而是使用 collection-repeat..它工作正常

这是我的工作片段...

<ion-content overflow-scroll="false">
<ion-list>

<div class="card" collection-repeat="product in products" scroll="atNewArticle($index)" scroll-item="atNewArticle($index)" id='product_list'>

<div class='row'>
<div class="col col-25">
<img ng-src={{product.ImagePath}} style="width:70px;height:70px"></img>
</div>


</div>
</ion-list>
</ion-content>

关于javascript - 在 Controller 中滚动后获取 ng-repeat 的索引 - Angular 、 Ionic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33142989/

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