gpt4 book ai didi

javascript - 从 addEventListener 函数向 ng-repeat 添加数据不会更新 DOM

转载 作者:行者123 更新时间:2023-12-01 05:16:18 25 4
gpt4 key购买 nike

当我使用 addEventListener 函数向 $scope 中的数组添加数据时,新数据可以记录到控制台中,但是 ng-repeat 重复的 DOM 没有改变

angular.module('module', [])
.controller('controller', function ($scope) {
$scope.init = function () {
$scope.list = [1,2,3,4,5,6,7,8,9,10]
document.addEventListener('scroll', function (e) {
// the bottom of window
var windowBottom = e.target.documentElement.scrollTop + e.target.documentElement.clientHeight
// top of loading div
var loadingTop = document.getElementById('loading').offsetTop
if (windowBottom >= loadingTop) {
$scope.list.push($scope.list.length + 1)
console.log($scope.list)
}
})
}
<!DOCTYPE html>
<html lang="en" ng-app="module">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div style="height: 100%" ng-controller="controller" ng-init="init()">
<div id="wrapper">
<div id="scroller">
<div class="card" ng-repeat="i in list">{{i}}</div>
<div id="loading" class="loading">Loading...</div>
</div>
</div>
</div>
</body>
<script src="angular.min.js"></script>
<script src="main.js"></script>
</html>
html, body{
margin: 0;
padding: 0;
height: 100%;
}
body{
background-color: #eee;
}
#wrapper{
height:calc(100% - 1px);
}
.card{
background-color: #fff;
height: 80px;
line-height: 80px;
text-align: center;
margin-bottom: 15px;
}
.loading{
height: 80px;
background-color: #e6e6e6;
line-height: 80px;
text-align: center;
}

我期望的是,当我滚动到文档底部的加载 div 时,新数据将被推送到 $scope 的数组中,并且 ng-repeat 的 DOM 重复将同时改变,但它不会'工作。

列表数据可以记录在console.log中,但是DOM没有变化

运行演示:JS Bin

最佳答案

Hello Friend $scope 当我们在 Angular 代码中调用事件处理程序时不会更新。为此,您必须在事件处理程序之后使用 $scope.apply

angular.module('module', [])
.controller('controller', function ($scope) {
$scope.init = function () {
$scope.list = [1,2,3,4,5,6,7,8,9,10]
document.addEventListener('scroll', function (e) {
// 窗口底部
var windowBottom = e.target.documentElement.scrollTop + e.target.documentElement.clientHeight
// 加载div顶部
var loadingTop = document.getElementById('loading').offsetTop
if (windowBottom >= loadingTop) {
// if ($scope.flag) {
$scope.list.push($scope.list.length + 1)
console.log($scope.list)
// }
}
$scope.$apply()
})
}

})
html, body{
margin: 0;
padding: 0;
height: 100%;
}
body{
background-color: #eee;
}
#wrapper{
height:calc(100% - 1px);
}
.card{
background-color: #fff;
height: 80px;
line-height: 80px;
text-align: center;
margin-bottom: 15px;
}
.loading{
height: 80px;
background-color: #e6e6e6;
line-height: 80px;
text-align: center;
}
<!DOCTYPE html>
<html lang="en" ng-app="module">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>
<body>
<div style="height: 100%" ng-controller="controller" ng-init="init()">
<div id="wrapper">
<div id="scroller">
<div class="card" ng-repeat="i in list track by $index">{{i}}</div>
<div id="loading" class="loading">加载中</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
</body>
</html>

`

关于javascript - 从 addEventListener 函数向 ng-repeat 添加数据不会更新 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55351718/

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