Torben
Torben

Reputation: 5494

Accessing single json value via Angular?

my API returns this value:

{"id":1,"name":"Test"}

In Angular I did this call to retrieve that value:

$scope.supplier = Suppliers.query({supplierId: $routeParams.id});

But now in my template I do this:

{{supplier.name}} 

Even {{ supplier }} does not give me anything.

but this is empty. Why is this the case?

Thanks!

Upvotes: 0

Views: 38

Answers (2)

Anushka Ahir
Anushka Ahir

Reputation: 146

var supplier = Suppliers.query({supplierId: $routeParams.id},{_id:0,name:1});

$scope.supplier=supplier[0].name;

Upvotes: 0

Nachshon Schwartz
Nachshon Schwartz

Reputation: 15795

The query function returns a callback, try this:

Suppliers.query({supplierId: $routeParams.id}, function(suppliers) => {
    $scope.supplier = suppliers[0];
});

Since query returns a list you need to take only the first from the list.

Upvotes: 1

Related Questions