Show image / background on md-select / md-options on hover in AngularJS 1.6

喜欢而已 提交于 2019-12-10 11:37:40

问题


I'm trying to put a hover on AngularJS material md-select dropdown where in on hover it shows a image next to it in a div/span. I'm unising the below code but still it's not coming:

<md-select id="{{mapping.id}}" ng-model="mapping.value">
  <md-option ng-value="option" 
             ng-mouseenter="show = true" 
             ng-mouseleave="show = false" 
             ng-repeat="map in mapping.values" 
             ng-selected="$first">{{maping.options[$index]}}</md-option>
  <span ng-show="show">
      <img src="{{mapping.image[$index]}}" 
           align-"right" width="60px" 
           height="40px" 
           vertical-align="top"/>
  </span>
</md-select>

and using the below in app:

scope.shopw = false;

Can we have a watch on hover? to have something like below, http://plnkr.co/edit/j5LBYQCXvN9LhXt25tTb?p=preview


回答1:


You could achieve that by writing an own directive like I did for you. Please check this runnable fiddle demo.

View:

<div ng-app="sandbox" ng-controller="myCtrl">
  <div layout-gt-sm="row" layout="column" layout-margin>
    <main flex-md="60" flex-order-gt-sm="2">
      <h1 class="md-title">
        Main Content
      </h1>
      <div layout="row" layout-xs="column">
        <div flex="60">
          <md-select ng-model="mapping.value">
            <md-option ng-value="map.name"
                       ng-repeat="map in mapping.values" 
                       ng-selected="$first"
                       container-image
                       class="image-container"
                       image="map.image">{{map.name}}</md-option>
          </md-select>
        </div>
      </div>
    </main>
  </div>
</div>

CSS

._md {
  overflow: visible;
}

.preview-image {
  position: absolute;
  height:100px;
  width:100px;
  right: -140px;
}

Application:

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

myApp.controller('myCtrl', function($scope) {
  $scope.mapping = {
    value: null,
    values: [ {
        name: "1",
        image: "http://placehold.it/100x100"
      }, {
        name: "2",
        image: "http://placehold.it/100x100"
      }, {
        name: "3",
        image: "http://placehold.it/100x100"
      },
    ]
  }
});

myApp.directive('containerImage', function($compile) {
  return {
    restrict: 'A',
    scope: {
      image: "="
    },
    link: function (scope, element, attrs) {

      element.on('mouseover', function (e) {
        if (element.find("img").length === 0) {
          var imageElement = angular.element('<img class="preview-image" src="'+ scope.image +'" height="100" width="100" />');
          element.append(imageElement);
          $compile(imageElement)(scope);
        } else {
          element.find("img").attr('style', 'display:block');
        }
      });

      element.on('click', function (e) {
          element.find("img").attr('style', 'display:none');
      });

      element.on('mouseleave', function (e) {
          element.find("img").attr('style', 'display:none');
      });
    }
  }
});


来源:https://stackoverflow.com/questions/44085782/show-image-background-on-md-select-md-options-on-hover-in-angularjs-1-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!