Angular: Access resource value in controller

后端 未结 1 362
春和景丽
春和景丽 2020-12-03 12:31

I\'m terrible at javascript and very new to Angular so do bear with me.

My server is returning this:

{\"latitude\": 3.172398, \"name\": \"Event\", \"         


        
相关标签:
1条回答
  • 2020-12-03 13:21

    From the docs:

    It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

    So your logging wont work unless you put it in a callback function, like this:

    function mapCtrl($scope, Event) {
      Event.get({eventId: $scope.eventId},function(eventDetail){
        //on success callback function
        console.log(eventDetail);
        console.log(eventDetail.latitude);
      });
    }
    

    If you for some reason don't want to work with a resource you can use the $http service:

    $http.get(url).then(function(response){console.log(response.data);});
    
    0 讨论(0)
提交回复
热议问题