javascript - Angular Directive(指令) mouseenter/mouseleave 工作但在 mouseleave 后不设置为初始状态

标签 javascript jquery angularjs angularjs-directive angularjs-scope

我有一个指令在模板上显示学生信息列表,然后在鼠标输入时显示其他学生信息。我希望能够返回到 mouseleave 的初始状态。

尝试了所有资源,但运气不佳。

html - 这是我注入(inject)指令的地方

<div ng-repeat="student in studentPortfolio">
<portfolio-view student="student"></portfolio-view>
</div>

html 指令模板

<div class="outer-box">
  <img src="{{student.picture}}" alt="{{student.name.first}} {{student.name.last}}" style="width: 200px; height: 200px">
  Name: {{student.name.first}} {{student.name.last}}
  <br>Bio: {{student.Bio}}
  <br>
  Skills:
<div ng-repeat="skill in student.skills">
{{skill.title}}
  </div>

  <br>
</div>

指令

app.directive('portfolioView', function() {
  return {
    restrict: 'E',
    scope: {
      student: "="
    },
    templateUrl: '/html-templates/hoverPortfolio.html',
    link: function(scope, elem, attrs) {
      //gets the first project and shows it
      var project = scope.student.projects;
      var firstProject = project[0];
      var fp_name = firstProject.name;
      var fp_type = firstProject.projectType;
      var fp_description = firstProject.description;
      //gets the second project and shows it
      var secondProject = project[1];
      var sp_name = secondProject.name;
      var sp_type = secondProject.projectType;
      var sp_description = secondProject.description;
      //the template that shows the second project
      var newHtml =
        '<div class="projects outer-box"><div class="firstproject"> Project Name: ' +
        fp_name + '<br>Type: ' + fp_type + '<br>Description: ' +
        fp_description +
        '</div><br><div class="secondproject"> Project Name: ' +
        sp_name + '<br>Type: ' + sp_type + '<br>Description: ' +
        sp_description +
        '</div> </div>';

      elem.on('mouseenter', function() {
        elem.html(
          newHtml
        )
      });

      elem.on('mouseleave', function() {
      //return to intial state
      });
    }
  }
});

最佳答案

我没有你的数据,但是 ng-show 可以工作,就像这个 fiddle .

这是一个更简单的变体。如果您的模板包含您希望显示或隐藏的部分,并带有一个 ng-show 变量,那么您的指令可能相当简单:

return {
    restrict: 'EAC',
    replace: true,
    template: '<div><div ng-show="show">show</div><div ng-show="!show">hide</div></div>',
    link: function (scope, element, attrs, controller) {
        scope.show = true;
        element.on('mouseenter', function () {
            scope.$apply(function () {
                scope.show = false;
            });
        });
        element.on('mouseleave', function () {
            scope.$apply(function () {
                scope.show = true;
            });
        });
    }
};

关于javascript - Angular Directive(指令) mouseenter/mouseleave 工作但在 mouseleave 后不设置为初始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750751/

相关文章:

javascript - Django 身份验证 : CSRF Failed

javascript - 如何查找并匹配 JSON 对象数组中的特定值?

javascript - Puppeteer 从 pdf 下载链接获得响应

jquery - 使用占位符 html5 时出现奇怪的 Javascript 问题

jquery - 过渡不适用于高度 :0px

jquery - ToggleClass 基于 li 标签内的图像高度

java - 使用 AngularJS 显示 Spring REST 数据

angularjs - 如何在 angularjs 中自动播放视频?

javascript - 使用 Javascript 添加列表

javascript - jQuery 变量结果为 "undefined",即使是全局的