I am using an API to search for songs but I just wanted to simply show 1 song from the array in my html file by using an expression in angular.js. Now I have bumped into some problems because I want to use my JSON object out of the AJAX call and in the controller.
(function() {
var app = angular.module('songSelector', []);
app.controller('playedTracksCtrl', function() {
// this.songs = jsonObjectPlayedTracks;
$.ajax({
url: "http://api.q-music.be/1.2/tracks/plays?limit=20",
dataType: "jsonp",
jsonp: "callback",
success: function(jsonObject){
//console.log(jsonObject);
//console.log(jsonObject["played_tracks"][0]);
var jsonObjectPlayedTracks = jsonObject["played_tracks"];
console.log(jsonObjectPlayedTracks);
}
});
});
})();
<!DOCTYPE html>
<html ng-app="songSelector">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/custom.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</head>
<body>
<header>
<h1 class="text-center">Vind info over de laatste 50 liedjes</h1>
<h2 class="text-center"> - vul in - </h2>
</header>
<div class="list-group" ng-controller="playedTracksCtrl as overzicht">
<div class="list-group-item">
<h3>{{overzicht.songs[0].title}}</h3>
</div>
</div>
<p> {{"Hello, Angular!"}} </p>
</body>
</html>
$http
or$resource
instead of jQuery. Angular is a complete framework with support for almost all things you need. jQuery is just DOM manipulation, and useful functions. – Callum Linington Jan 23 at 10:28