简体   繁体   English

angularJs-具有自定义ID的ngRepeat

[英]angularJs - ngRepeat with custom id

Edit: 编辑:

So, after messing around with the code and following everyones advice, I found out that, indeed, the id -does- work, but somethign else blocks it. 因此,在弄乱了代码并听取了每个人的建议之后,我发现id确实起作用了,但是其他一些阻止了它。

This is the entire line of code, now: where is the problem located? 现在,这是完整的代码行:问题在哪里?

<tr id="{{item.id}}" 
    ng-mouseover="hovering(item.id)" 
    ng-mouseleave="leaving(item.id)"  
    ng-show="showRow(item)" 
    ng-repeat="item in elements| limitTo: limiter"  
    data-id="{{item.id}}" 
    data-parent="{{item.parent}}">

HTML: HTML:

<table>
    <tr ng-repeat="item in users| limitTo: limiter" id="{{item.id}}">
        <td>{{item.name}}</td>
        <td>{{item.email}}</td>
    </tr>
</table>

DATA: 数据:

$scope.users = [
    { id: 1, name: "user", email : ""},
    { id: 2, name: "anotherUser", email: ""}];

Problem: 问题:

the ID stays "{{item.id}}" and isn't converted to, eg: "1". 该ID保留为"{{item.id}}" ,并且不会转换为例如:“ 1”。 I tried with ng-attr-id as well, but it didn't work either. 我也尝试了ng-attr-id ,但是也没有用。

Why isn't the ID "transformed" to what it should be? 为什么ID不能"transformed"为原样?

To bring the problem a bit further: if I do data-id="{{item.id}} , it DOES work. 让问题更进一步:如果我执行data-id="{{item.id}} ,它确实起作用。

All seems to Work 一切似乎都在工作

 angular.module('app', []) .controller('ctrl', ["$scope", function($scope) { $scope.limiter = 4; $scope.users = [{ id: 1, name: 'n1' }, { id: 2, name: 'n2' }, { id: 3, name: 'n3' }, { id: 4, name: 'n4' }, { id: 5, name: 'n5' }, { id: 6, name: 'n6' }]; } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <table ng-app="app" ng-controller="ctrl"> <tr ng-repeat="item in users| limitTo: limiter" id="{{item.id}}"> <td>Id - {{item.id}}</td> <td>Name - {{item.name}}</td> </tr> </table> 

My previous answer is very poor, But some one put up vote. 我以前的回答很差,但是有人投了赞成票。 lol. 大声笑。

okay finally i got the issue. 好吧,终于我明白了。

You need to change var users = [...] to $scope.users=[...]; 您需要将var users = [...]更改为$scope.users=[...];

Update 更新资料

  • Did you assign controller and ng-app in you html side? 您是否在html端分配了controllerng-app

  • try to use $scope.$apply() in your controller. 尝试在控制器中使用$scope.$apply()

    $scope.users = [ { id: 1, name: "user", email : ""}, { id: 2, name: "anotherUser", email: ""}]; $scope.$apply();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM