javascript - 使用 ng-repeat 解析 AngularJS 中的错误

标签 javascript angularjs

我有以下代码片段,它给了我解析错误。

<a class="pointer" role="menuitem" ng-repeat="rec in cfg.popupAssoc" ng-click="followSession(\"{{$index}}\")" tabindex="{{$index + 2}}"
    {{rec.popupEntry}}
</a>

我将\"{{$index}}\"更改为this后,就没有编译错误了。

知道为什么吗?谢谢。

更新1

基本问题是,我想将一些东西(可能是带有空格的字符串)传递给函数,但无法找到一种方法来做到这一点。下面的代码是一个天真的尝试,但它给出了 Angular 解析错误。

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr >
    <td ng-click="dum();">ONE</td>
    <td>TWO</td>
  </tr>
  <tr ng-repeat="x in names">
    <td ng-click="dum({{x.Name}});" myid="{{$index + 1}}">{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
obj1 = "haha";
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function (response) {$scope.names = response.records;});
    $scope.dum = function(x) { obj1 = x; console.log(x); }
});

</script>

</body>
</html>

最佳答案

您需要更新

<a class="pointer" role="menuitem" ng-repeat="rec in cfg.popupAssoc" ng-click="followSession(\"{{$index}}\")" tabindex="{{$index + 2}}"
    {{rec.popupEntry}}
</a>

<a class="pointer" role="menuitem" ng-repeat="rec in cfg.popupAssoc" ng-click="followSession($index)" tabindex="{{$index + 2}}"
    {{rec.popupEntry}}
</a>

关于javascript - 使用 ng-repeat 解析 AngularJS 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482704/

相关文章:

javascript - Nodejs 和 Express,从工作线程使用 res.send()

javascript - 如何从 Redux 存储获取状态

angularjs - 如何调用angular数据表的销毁函数?

javascript - AngularJS:调用 save() 后取消覆盖值 $resource 对象;

Angularjs - 错误 : [$injector:unpr]

javascript - 如何使用动态 html 和注入(inject) Angular 引导弹出窗口的指令?

线程

javascript - 从没有副作用的原型(prototype)函数创建有副作用(就地)的函数

javascript - tagName 返回未定义

angularjs Material 设计嵌套列表