javascript - AngularJS, $resource, get the object from .query() -


need 1 object .query(), service:

angular.   module('core.users').   factory('user', ['$resource',     function($resource) {       return $resource('users/users.json', {}, {         query: {           method: 'get',           isarray: true          }        });       }     ]); 

component:

'use strict';  angular.   module('userdetail').   component('userdetail', {     templateurl: 'user-detail/user-detail.template.html',     controller: ['$routeparams', 'user', '$filter',       function taskdetailcontroller ($routeparams, user, $filter) {         var vm = this;          vm.id = parseint($routeparams["id"], 10);         vm.users = user.query(function() {           vm.user = $filter('currentuser')(vm.users, vm.id);         });       }     ],      controlleras: 'vm'   }); 

need 1 object array, 'currentuser', $routeparams id, problem - vm.user undefined in controller scope, here

vm.users = user.query(function() {   vm.user = $filter('currentuser')(vm.users, vm.id); // ok, got object   }); console.log(vm.user) // undefined 

it's ok view, can display properties, need make .put() request server solo object, when of user props changed...

from https://docs.angularjs.org/api/ngresource/service/$resource

http "class" actions: resource.action([parameters], [success], [error])

you should write:

vm.users = user.query(); vm.users.$promise.then(function(response){   vm.user = $filter('currentuser')(vm.users, vm.id);   console.log(vm.user); // should log desired result }); console.log(vm.user); //this undefined since called before promise resolved 

Comments