0

I have created a controller (movies.js) as below-

'use strict';
angular.module('clientApp')
  .controller('MoviesCtrl', function ($scope) {
    $scope.movies = [
        {
        title:'Star Wars!',
        url:'http://www.google.com'
        },
        {
        title: 'Star Wars2!',
        url: 'http://www.google.com'
        },
        {
        title: 'Star Wars3!',
        url: 'http://www.google.com'
        }
    ];
  });

Now I'm trying to access the values of each objects using ng-repeat in movies.html-

<table class="table table-striped">
<thead>
    <th>Title</th>
    <th>URL</th>    
</thead>
<tbody>
<tr ng-repeat="movie in movies">
<td> {{ movie.title }} </td>
<td> {{ movie.url }} </td>
</tr>
</tbody>
</table>

However, the values are not populated as expected. Can someone please provide any tips on where should I look to fix it?

3
  • $scope.movies instead of this.movies Commented Aug 27, 2015 at 4:31
  • Tried that too, but does not work Commented Aug 27, 2015 at 4:32
  • 2
    Did you specify ng-controller and ng-app in your html markup? Commented Aug 27, 2015 at 4:41

5 Answers 5

1

Since you are assigning the array to this so

var app = angular.module('my-app', [], function() {})

app.controller('AppController', function($scope) {
  this.movies = [{
    title: 'Star Wars!',
    url: 'http://www.google.com'
  }, {
    title: 'Star Wars2!',
    url: 'http://www.google.com'
  }, {
    title: 'Star Wars3!',
    url: 'http://www.google.com'
  }];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
  <div ng-controller="AppController as ctrl">
    <table class="table table-striped">
      <thead>
        <th>Title</th>
        <th>URL</th>
      </thead>
      <tbody>
        <tr ng-repeat="movie in ctrl.movies">
          <td>{{ movie.title }}</td>
          <td>{{ movie.url }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>


Or assign the array to the scope variable

var app = angular.module('my-app', [], function() {})

app.controller('AppController', function($scope) {
  $scope.movies = [{
    title: 'Star Wars!',
    url: 'http://www.google.com'
  }, {
    title: 'Star Wars2!',
    url: 'http://www.google.com'
  }, {
    title: 'Star Wars3!',
    url: 'http://www.google.com'
  }];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
  <div ng-controller="AppController">
    <table class="table table-striped">
      <thead>
        <th>Title</th>
        <th>URL</th>
      </thead>
      <tbody>
        <tr ng-repeat="movie in movies">
          <td>{{ movie.title }}</td>
          <td>{{ movie.url }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

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

Comments

0

You should try this

'use strict';
angular.module('validationApp' ,[])
  .controller('MoviesCtrl', function ($scope) {
    $scope.movies = [
        {
        title:'Star Wars!',
        url:'http://www.google.com'
        },
        {
        title: 'Star Wars2!',
        url: 'http://www.google.com'
        },
        {
        title: 'Star Wars3!',
        url: 'http://www.google.com'
        }
    ];
});

Comments

0

it should be

<table class="table table-striped">
<thead>
    <th>Title</th>
    <th>URL</th>    
</thead>
<tbody>
<tr ng-repeat="movie in MoviesCtrl.movies">
<td> {{ movie.title }} </td>
<td> {{ movie.url }} </td>
</tr>
</tbody>
</table>

or you should bind it to $scope.movies

Comments

0

Still you won't be able to load external URLs. You need to make a filter to whitelist the external links. Refer below code.

angular.module('myApp')
  .filter('trustUrl', function ($sce) {
    return function(url) {
    return $sce.trustAsResourceUrl(url);
  };
});

<td> {{ movie.url | trustUrl}} </td>

Comments

0

You need to inject $scope into your controller, and use that to bind your array to your view

.controller('MoviesCtrl', function($scope){
    $scope.movies = [
        {
            title:'Star Wars!',
            url:'http://www.google.com'
        },{
            title: 'Star Wars2!',
            url: 'http://www.google.com'
        },{
            title: 'Star Wars3!',
            url: 'http://www.google.com'
        }
    ];
});

Also, don't forget that you need to specify in your view that there is a controller you want to use. For example, in your html:

<div ng-controller="MoviesCtrl">
    <!-- any html you associate with your controller -->
</div>

Comments

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.