Angularjs map array to another array

后端 未结 4 1737
忘了有多久
忘了有多久 2021-01-15 14:18

I have two arrays, Users and Employments like so:

Users       = [{id:1, name: \"ryan\"}, {id:2, name:\"Julie\"}]
Employment         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-15 14:37

    If you wanted to match up the two following arrays purely with a template you could take the following arrays

    Users       = [{id:1, name: "ryan"}, {id:2, name:"Julie"}]
    Employments = [{user_id: 1, title: "manager"}, {user_id: 2, title: "Professor"}]
    

    And nest a repeat like:

        
  • {{user.name}}:{{employment.title}}
  • Two more nice little thing to do to avoid any risk of getting those brackets showing on a slow page load is to use the ng-bind and prefix the attributes with data so its with the html spec

            
  • :
  • I know you didn't have the need for anything but the name, but figured a quick example of using the outer loop in the inner still could be helpful. Also this would be the case for ng-init if you needed to reference the the $index of the outer ng-repeat from the inner, but that might be more than you're looking for here.

提交回复
热议问题