I am looking to merge two arrays together. Here, I have a piece of code where I want to extract each user row from the foo array and create a new array.
UserService.GetUserFoo(id)
.success(function (data) {
data = angular.fromJson(data); //[{id:1, u_id:1, foo:"a"}{id:2, u_id:2, foo:"b"}]
angular.forEach(data, function(user){
getUser(user.u_id) }); //FUNCTION CALL
}).
error(function(error) {
//do something
});
In this part of the code, the getUser function is invoked in the GetUserFoo service to populate a new array named $scope.users
$scope.users = []; //[{id:1, name:"Ed"},{id:2, name:"Jim"}]
var getUser = function(id) {
UserService.GetUserById(id)
.success(function (data) {
data = angular.fromJson(data);
$scope.users.push(data); //
}).error(function(error) {
//do something
});
};
QUERY --> How can I insert each foo field into the corresponding $scope.users object so that in my view I can display something like this
//[{id:1, name:"Ed", foo:"a"}{id:2, name:"Jim", foo:"b"}]
<li ng-repeat="user in users">
<h2>{{user.name}}</h2>
<p>{{user.foo}}</p>
</li>