Rawle Juglal Rawle Juglal - 1 month ago 10
AngularJS Question

Can't reach field inside an object in my angular controller

I'm using a factory to get a television show from a database. I wanted to send just the episodes field from this object to a angular service but I keep getting undefined using dot notation and bracket notation. Can someone explain how to get this field.

Controller

angular.module('showApp.controllers', []).controller('ShowViewController', function($scope, $stateParams, Show, $http) {
$scope.show = Show.get({ id: $stateParams.id }); //Get a single show.Issues a GET to /api/shows/:id

console.log($scope.show)
console.log($scope.show.episodes)
})


Console Output

m
$promise:d
$resolved:true
__v:0
_id:"581a5b82ff9e9f10f22afff7"
airs_on:Array[1]
episodes:Array[11]
genre:Array[3]
network:"Netflix"
poster:"https://images-na.ssl-images-amazon.com/images/M/MV5BMTk5NTk1Mzg3Ml5BMl5BanBnXkFtZTcwNDAyNzY3OA@@._V1._CR25,3,1010,1343_SY1000_CR0,0,752,1000_AL_.jpg"
program_time:60
rated:"TV-MA"
streams_on:Array[3]
title:"Black Mirror"
yearBegin:2011
__proto__:Object

undefined

Answer

you can wait for results by:

$scope.show.$promise.then(function(show) {
  console.log(show.episodes);
})

you should call the service in the then function.