eilidh eilidh - 2 years ago 67
AngularJS Question

AngularJS Communication between ng-repeat and controller

I've just started using AngularJS, and as a project I decided to build a simple app for managing bookmarks. I want to be able to maintain a list of bookmarks and add/remove items. I'm using Django with Django REST framework, and Angular.

So far I've written a service to grab the bookmarks from the database, and I can print them to the console from my controller, but ng-repeat doesn't seem to be seeing them.

Here's my code for the service:

.factory('BookmarkService', ["$http", function ($http) {
var api_url = "/api/bookmarks/";
return {
list: function () {
return $http.get(api_url).then(function (response) {
return response.data

And for the controller:

["$scope", "BookmarkService", function ($scope, BookmarkService) {
$scope.bookmarks = BookmarkService.list();

And here's the HTML:

<div ng-controller="ListController as listCtrl">
<md-list-item ng-repeat="bookmark in listCtrl.bookmarks">
<div class="md-tile-content">
<p>{[{ bookmark.name }]} - {[{ bookmark.url }]}</p> // used interpolateProvider to allow "{[{" instead of "{{"
<md-divider ng-if="!$last"></md-divider>

When I print to the console from the controller I can see a promise object but ng-repeat isn't repeating:
image of promise object

I'd really appreciate if someone could help me to find my mistake and to understand why it is happening. I'm still not entirely comfortable with how all these parts fit together.

Thanks for your time!

Answer Source

There's two problems that I see with the code in question.

The first is that using the controller as syntax (ng-controller="ListController as listCtrl") requires properties to be bound to the controller instance and not to the scope if you address them using the controller name. In your case,

  ["BookmarkService", function (BookmarkService) {
    this.bookmarks = BookmarkService.list();

The second is that you are assigning a promise to your $scope.bookmarks property. The repeater is expecting an array of objects to iterate over. You really want to assign the value resolved by the promise to $scope.bookmarks.

Instead of this

$scope.bookmarks = BookmarkService.list();

Do this

  this.bookmarks = result;

The final version of your controller should look something like this

  ["BookmarkService", function (BookmarkService) {
    this.bookmarks = BookmarkService.list();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download