简体   繁体   English

angularjs ng-repeat不在ng-model中显示我的数据

[英]angularjs ng-repeat not showing my data in ng-model

I have this 我有这个

ng-repeat="directive in vm.directives">

in which that loop properly displays all of my data 在那个循环中正确显示我的所有数据

Inside that loop i can do this 在该循环内,我可以执行此操作

{{vm.question.directiveassoc[0].SortOrder}}

and that works 那行得通

but when i try to do 但是当我尝试去做

{{vm.question.directiveassoc[directive.Id].SortOrder}}  that does not work

nor more importantly I want my ng-model to display the data in my object 更重要的是,我不希望我的ng模型在对象中显示数据

ng-model="vm.question.directiveassoc[directive.Id].SortOrder"

Why doesn't that work , shouldn't it display data from my object? 为什么这样不起作用,它不应该显示我的对象中的数据吗?

{
  "Id": 105,
  "Name": "yyyyyyyyyyyyy",
  "Description": "yyyyyyyyyyyyyy",
  "Active": true,
  "$$hashKey": "object:98",
  "directiveassoc": [
    {
      "Id": 96,
      "QuestionId": 105,
      "DirectiveId": 1,
      "SortOrder": 99,
      "data": "yyyyyy"
    }
  ]
}

Update 更新

Ok, this works 好的,这有效

   ng-model="vm.question.directiveassoc[directive.Id -1].SortOrder"

Problem is that looping over vm.directives , is a list of objects that start with 1 , since they are in order 1 to many , and since my other object of vm.question.directiveassoc contains the same directive id , along with sortorder .. that works if i -1 问题是vm.directives循环是一个以1开头的对象的列表,因为它们的顺序是1到很多,并且因为我的vm.question.directiveassoc其他对象包含相同的指令ID和sortorder ..如果我-1可以工作

So since object starts with 0 , i just subtract 所以既然对象从0开始,我就减去

This feels icky / hacky though 虽然这感觉很邪恶

What is a better way to really do this loop with different objects? 真正对不同对象执行此循环的更好方法是什么?

It looks like your directive has an Id of 1, not 0. 看起来您的指令的Id为1,而不是0。

I suggest that you create a method on the controller to get the sort order for a given directive id, and then do a proper array search to find the entry in the directiveassoc array with the correct DirectiveId . 我建议您在控制器上创建一个方法来获取给定指令ID的排序顺序,然后执行适当的数组搜索以在带有正确DirectiveIddirectiveassoc数组中找到条目。

If using Lodash or Underscore, the _.find() method should be what you need, otherwise write your own. 如果使用Lodash或Underscore,则_.find()方法应该是您所需要的,否则请编写您自己的方法。

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

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