简体   繁体   中英

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-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

So since object starts with 0 , i just subtract

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.

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 .

If using Lodash or Underscore, the _.find() method should be what you need, otherwise write your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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