ng-repeat 和 mouseover 事件

3

这似乎是作用域问题,但我不确定。我的目标是突出显示表中的一行。这意味着任何先前突出显示的行都将返回到未突出显示状态。行是使用ng-repeat指令创建的,如下所示:

<div id="myFedContents" style="height:320px" ng-controller="Controller2" class="scroller">
    <table border="0" class="span12 table table-condensed" style="margin-left:0px" id="tblData">
        <thead>
            <tr><th>Year</th><th>Name</th><th>Useful Flag</th></tr>
        </thead>
        <tbody id="allRows">
            <tr ng-repeat="item in itemlist | filter:thisText" ng-style="myStyle">            <td class="span1" valign="top"><a tabindex="-1" href="#">{{item.year}}</a></td>
                <td id="{{item.id}}">                                   <a tabindex="-1" href="#" ng-click="myStyle={'background-color':'#cccccc'};">{{item.name}}</a>
                </td>                                     <td>
                    <a href="#" class="btn btn-small btn-link">{{item.usefulflag}</a> 
                </td>                                 </tr>                             
        </tbody>
    </table>
</div>

i have code in a .js file that looks like this:

$("tr").mouseenter(function(){
    alert("mouseenter");
});
表头中的行与警报交互,但由ng-repeat创建的行没有反应。如何纠正?
1个回答

3
您可以通过使用 ng-class 结合 ng-mouseenter 和 ng-mouseleave 实现此效果,如下所示:
<div id="myFedContents" style="height:320px" ng-controller="Controller2" class="scroller">
  <table border="0" class="span12 table table-condensed" style="margin-left:0px" id="tblData">
    <thead>
      <tr>
        <th>Year</th>
        <th>Name</th>
        <th>Useful Flag</th>
      </tr>
    </thead>
    <tbody id="allRows">
      <tr ng-repeat="item in itemlist | filter:thisText" ng-style="myStyle" ng-class="highlightclass" ng-mouseenter="highlightclass='highlight'" ng-mouseleave="highlightclass=''">
        <td class="span1" valign="top"><a tabindex="-1" href="#">{{item.year}}</a>
        </td>
        <td id="{{item.id}}"> <a tabindex="-1" href="#" ng-click="myStyle={'background-color':'#cccccc'};">{{item.name}}</a>
        </td>
        <td>
          <a href="#" class="btn btn-small btn-link">{{item.usefulflag}</a> 
        </td>
      </tr>
    </tbody>
  </table>
</div>

在这种情况下,您不需要使用jQuery语法。如果您还没有阅读,您应该阅读 https://dev59.com/YGUp5IYBdhLWcg3w3KXe#15012542


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接