gpt4 book ai didi

javascript - 外部元素在外部 ng-repeat 中访问数据

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:35 24 4
gpt4 key购买 nike

我有一个包含 ng-repeat details 的列表。
当我将鼠标悬停在其中一个 li 上时,我希望位于 ng-repeat 之外的名为 background 的 div 将其背景更改为 details 的网址。

但我不知道如何让外部元素访问 ng-repeat 的数据。

plunkr:https://plnkr.co/edit/lGRfE9YGYN645Ztpr94K?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

$scope.details = [
{ "name": "Employees", "url":"https://i.pinimg.com/736x/76/47/9d/76479dd91dc55c2768ddccfc30a4fbf5--pikachu-halloween-costume-diy-halloween-costumes.jpg" },
{ "name": "Support", "url":"https://i.pinimg.com/736x/76/47/9d/76479dd91dc55c2768ddccfc30a4fbf5--pikachu-halloween-costume-diy-halloween-costumes.jpg" }
];

});

HTML:

<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<div class="background">
<ul ng-repeat="detail in details">
<li>{{detail.name}}

</li>
</ul>
</div>
</body>

</html>

最佳答案

您可以在 li 元素上使用 ng-mouseover 并使用 ng-style 应用背景div

<div ng-style="{'background-image':'url(' + bgUrl + ')'}">

演示

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

$scope.details = [
{ "name": "Employees", "url":"https://i.pinimg.com/736x/76/47/9d/76479dd91dc55c2768ddccfc30a4fbf5--pikachu-halloween-costume-diy-halloween-costumes.jpg" },
{ "name": "Support", "url":"http://i.stack.imgur.com/IosVb.png" }
];
$scope.applybackground=function(detail){
$scope.bgUrl = detail.url;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css" />
</head>
<body ng-controller="MainCtrl">
<div ng-style="{'background-image':'url(' + bgUrl + ')'}">
<ul ng-repeat="detail in details">
<li ng-mouseenter="applybackground(detail)">{{detail.name}}
</li>
</ul>
</div>
</body>

</html>

关于javascript - 外部元素在外部 ng-repeat 中访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577946/

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