【问题标题】:Why is this <td ng-mouseenter="name='John'"> not working?为什么这个 <td ng-mouseenter="name='John'"> 不起作用?
【发布时间】:2017-08-13 08:41:57
【问题描述】:

下面的例子非常简单(在sp00m 之后编辑,对前面的例子响应正确):

index.html:(部分)

<table>
  <tr ng-repeat="r in [1,2,3]">
    <td ng-repeat="c in [1,2]" ng-mouseenter="name='John'">
      [{{r}},{{c}}]
    </td>
  </tr>
</table>
Hello {{name}}!

app.js:

app.controller('MainCtrl', function($scope) {
    $scope.name = 'World';
});

我期待将显示“世界”一词更改为“约翰”,但当我将鼠标悬停在单元格上时没有任何反应。

我在下面发布 plnkr 以显示问题。 我究竟做错了什么?!我错过了什么? AngularJS 1.5.x plnkr 使用的问题是不能处理&lt;td&gt; 中的ng-mouseenter?请注意,ng-repeat 不是问题 - 当我手动放置新行时,这也不起作用。

http://plnkr.co/edit/x1peSJyc50yqa1AM73GZ

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    其实我也有同样的问题,我避免使用像$scope.name 这样的普通范围变量,它可能会产生一些问题。所以在上面的 plunker 中,声明一个类似于 $scope.name = {firstName: 'World'} 的范围对象,而不是普通的范围变量。然后在 html 中使用这个ng-mouseenter="name.firstName='John';",它应该可以和这个Hello {{name.firstName}} 一起使用。

    【讨论】:

    • 亲爱的桑蒂 - 哇。当我更改为结构时,它可以工作。有点沮丧!我仍然想知道为什么。这是AngularJS的错误......?非常感谢您的回复。
    • 嗨@Grzegorz,很抱歉回复晚了,使用点符号(表示对象)始终是首选方法。请在stackoverflow.com/questions/12618342/…这里一一查看答案,你会很好理解的。
    【解决方案2】:

    它实际上是有效的,你只是没有在你的$scope 中定义alert

    $scope.alert = function (...) { ... };
    

    【讨论】:

    • 好的,我期待使用 javascript alert。简单替换'x=10' 并显示x 怎么样。实际上这对我不起作用,所以我用alert 替换了它。 PS。我会更新我的原始输入,但感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 2018-01-17
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    相关资源
    最近更新 更多