I'm learning AngularJS and I'm trying to insert a 'sanitised' title into a h2 within a ng-repeat, however I cannot work out how to access the data within the array in the object. Everything else is working fine, I just need the 'title' value.
HTML:
<div ng-repeat="question in questions" ng-show="!!questions.length" class="question-list">
<h2><a ng-href="{{question.link}}" title="{{question.title}}" target="_blank" ng-bind-html="title"></a></h2>
</div>
This is the JS:
var loadFeed = angular.module('loadFeed', ['ngSanitize']);
loadFeed.controller('feedController', ['$scope', '$http', function($scope, $http) {
$scope.questions = [];
$http({
method: 'GET',
url: 'https://api.stackexchange.com/2.2/questions?pagesize=10&order=desc&sort=votes&tagged=angular&site=stackoverflow'
}).then(function(feed) {
console.log('success');
console.log(feed);
$scope.questions = feed.data.items;
console.log($scope.questions);
$scope.title = $scope.questions.title; // This is what I need for the ng-bind
},function(error) {
console.log('error');
console.log(error);
});
}]);
This does return an individual value (the first item's title):
$scope.title = $scope.questions[0].title;
However, I need the result of this (it's blank):
$scope.title = $scope.questions.title;
I've tried an angular.forEach and a JS loop however this just repeats every heading within the one list item.
Is there anything I am missing?
$scope.title = $scope.questions.title;your are trying to access the property title of an array. This will return undefined. So, why don't you let$scope.title = $scope.questions[0].title;?