0

I have an API set-up, which delivers JSON data to $resource.

In my controller I am doing the following to query a specific row:

$scope.company = Companies.query({id: companyId});

However, if in my 'view' I then do:

{{ company.company_name }}

It doesn't show anything ... how do I assign the returned JSON data to the 'company' scope?

2 Answers 2

1

This is how I use $resource

In Companyservice.js

this.queryItem = function (){

    var deferred = $q.defer();
    setTimeout(function() {
        // deferred.notify('Saving data..');
        var items = Company.query({},function() {
            deferred.resolve(items.d.results);
        }, function(error){
            deferred.reject(error);
        });
    }, 1000);
    return deferred.promise;
};

and in controller

var promise = CompanyService.queryItem();
    promise.then(function(response){
        // for each item in response, push item to array
        angular.forEach(response, function(item){
            $scope.companys.push(item);
        });

    }, function(reason){
        console.log(reason);
});

Instead of pushing the returned item(s) to an array, you should set $scope.company = response;

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, have you tried to console.log Companies.query({id: companyId});

To verify the data is there? If so, you may fetch the data async. If so, unless it is wrapped in a $http you need to tell angular that the model has been updated so that the view can refresh, to do so just:

$scope.$apply();

1 Comment

The JSON data is returned - I can see this in Chrome's console. When doing the scope apply, it gives an error that a digest is already running - as only one apply/digest can be run at a time, to prevent bugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.