My goal is to retrieve JSON data from my server and display it on my website using the Ui-router extension. I am specifically looking to set up a master-detail layout.
Index.html
<input ng-model="manga.name" ng-change="searchManga()" id="search" type="search" placeholder="Enter Manga Name..." required>
<div class="row" ui-view="viewA">
<div class="col s8 offset-s1" ng-controller = "nbgCtrl">
<div class="row">
<div class="col s12 m6 l4" ng-repeat = "manga in mangas">
<div class="row">
<div class="col s5">
<a ui-sref="#/manga/{{manga.id}}" class="thumbnail">
<img src="/covers/{{manga.cover}}">
</a>
</div>
<div class="col s7">
<p>{{manga.name}}</p>
<a href="" class="waves-effect waves-light btn">
</a>
I have created a main page with thumbnails that link to detailed information pages for each item. Clicking on a thumbnail should load its corresponding data onto the page. Here's what I have implemented so far:
JS:
angular.module('ourApp',["ui.router"])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('list', {
url: "/",
controller: "ListCtrl",
templateUrl: "index.html",
}
)
$stateProvider
.state('detail', {
url: "/detail/:{{mangaid}}",
controller: "DetailCtrl",
views: {
"viewA": { templateUrl: "detail.html" },
}
}
)
})
.factory('Mangas', function($http){
var factory = {};
function getData(manganame, callback) {
var url = '/remote/data.php?callback=JSON_CALLBACK';
$http.get(url).success(function(data){
factory = data.results;
callback(data.results);
})
}
return {
list: getData,
find: function(name, callback) {
console.log(name);
var manga = cachedData.filter(function(entry) {
return entry.id == name;
})[0];
callback(manga);
}
};
})
.controller('ListCtrl', function($scope, $http, Mangas) {
$scope.manga = {
name: '' }
$scope.searchManga = function() {
Mangas.list($scope.manga.name, function(mangas) {
$scope.mangas = mangas;
});
}
})
.controller('mmgCtrl', function($scope, $http, $stateParams, Mangas) {
Mangas.find($stateParams.mangaid, function(manga) {
$scope.manga = manga;
});
})