0

I have an angular factory and controller as such:

angular.module('myApp', ['ui.router', 'ngResource'])
.factory('Widget', ['$http', function WidgetFactory($http) {
    return {
        all: function() {
            return $http({method: "GET", url: "/widgets"});
        }
    };
}])
.controller('StoreCtrl', ['$scope', 'Widget', function($scope, Widget) {
    $scope.widgets = Widget.all();
}]);

In my front-end I have

<div ng-controller="StoreCtrl">
    <li ng-repeat="widget in widgets">
        {{ widget.price }}
        {{ widget.name }}
    </li>
</div>

But nothing gets populated in my {{ widget.price }} and etc.
What am I missing?

9
  • print $scope.widgets in your controllers check whether it contains data or not Commented Mar 9, 2016 at 4:54
  • your can see your data if data is available by using this line console.log($scope.widgets); or use debugging mode in your browser. Commented Mar 9, 2016 at 5:08
  • There is data available. It's an array of objects. But the objects properties do not appear in their {{ widget.name }} in the ng-repeat. Commented Mar 9, 2016 at 5:13
  • its not a good practice but for now instead of $scope use $rootScope and try. Commented Mar 9, 2016 at 5:14
  • @VishalSingh that breaks it completely. Commented Mar 9, 2016 at 5:15

3 Answers 3

3

You are not quite resolving your promise as the framework expects. Check out the $q docs for some explanation on promises. We also need to set our view model value (scope) to the correct returned value from the response object, composed of the following...

  • data – {string|Object} – The response body transformed with the
  • transform functions. status – {number} – HTTP status code of the
  • response. headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate
  • the request. statusText – {string} – HTTP status text of the response.

Observe the following...

Widget.all().then(function (response) {
    $scope.widgets = response.data;
});

Furthermore, as a few observations, there is no need to name your factory function as you did with WidgetFactory, an anonymous function will do. You can also leverage the $http shortcut method get as well as such...

.factory('Widget', ['$http', function($http) {
    return {
        all: function() {
            return $http.get('/widgets');
        }
    };
}]);
Sign up to request clarification or add additional context in comments.

Comments

2

$http returns a promise. You need to assign the response data to $scope.widgets when the promise resolves. In the controller, try this:

Widget.all().then(function (data) {
    $scope.widgets = data;
});

13 Comments

So I got the same result. But I can't access the values inside of widgets when I ng-repeat="widget in widgets" and do {{ widget.name }}
this seems to be the most helpful answer here, since you need to resolve a promise to get your results. There is no need to call $apply, since we're entirely in the context of angular with this code in question. @gh0st it sounds like you're missing something minor, is there a css class affecting one of the <li> elements or something to alter it's visibility?
Also for the sake of verbosity in your example above, perhaps assign your var to data.data. That's generally a bad param name for the callback, you'll typically see response.data, for there is usually a data property on the returned object
@scniro the $scope.widgets = data.data did the trick. Thank you. Finally someone who knows what they're talking about. If you post your comment as an answer I will mark it.
@gh0st for completeness I posted my answer, along with some minor suggestions
|
0

First make sure you have an objects of Array at

  $scope.widgets; 

in your controller. you can print this line in your controller

console.log($scope.widgets);

to see if the data is available or not.

1 Comment

Can you see the data in controller ?

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.