2017-10-05 36 views
0

我有一個包含ng-repeat details的列表。
當我將鼠標懸停在li之一上時,我想要位於ng-repeat之外的名爲background的div將其背景更改爲details的網址。訪問外部ng-repeat中數據的元素

但我不知道如何讓外部元素訪問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="[email protected]" 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> 
+0

使用'NG-mouseenter'和懸停'NG-mouseleave'。將一個變量設置爲true/false,並在'ng-class'中使用該變量來添加您的自定義類。 –

+0

問題不在於使用這些,它是我不知道如何訪問ng-repeat項目。 – Snorlax

+0

該變量可通過控制器範圍訪問。 –

回答

1

您可以在立項目使用ng-mouseover並使用應用背景ng-stylediv

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

DEMO

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>

0

可以使用NG-鼠標可訪問控制器的詳細信息對象。

<body ng-controller="MainCtrl"> 
    <div class="background" ng-style="{'background-image':'url({{myBackgroundUrl}})'}"> 
    <ul ng-repeat="detail in details"> 
     <li ng-mouseover="onHover(detail)">{{detail.name}}</li> 
    </ul> 
    </div> 
</body> 

在控制器:

$scope.myBackgroundUrl = ''; 
$scope.onHover = function(detail) { 
    console.log(detail.url) 
    $scope.myBackgroundUrl = detail.url; 
} 
0

使用NG-鼠標懸停像

<div class="background" style="background-image:url('{{backGroundUrl}}')"> 
<ul > 
    <li ng-mouseover="change(detail.url)" ng-repeat="detail in details"> 
     {{detail.name}} 

    </li> 

    $scope.change= function(url){ 
     $scope.backGroundUrl=url; 
    } 

Here is working example