Service (factory) error - json api not displaying data - angularjs

I have spent like a good 4 to 6 hours on trying to solve my issue - maybe I am looking at it completely wrong.
controller.js file:
'use strict';
/* Controllers */
var bookControllers = angular.module('bookControllers', []);
bookControllers.controller('BookListCtrl', ['$scope', 'Book',
function($scope, Book) {
$scope.books = Book.query();
$scope.orderProp = 'name';
}]);
bookControllers.controller('BookDetailCtrl', ['$scope', '$routeParams', 'Book',
function($scope, $routeParams, Book) {
$scope.book = Book.get({bookId: $routeParams.bookId}, function(book) {
$scope.mainImageUrl = book.avatar;
});
$scope.setImage = function(imageUrl) {
$scope.mainImageUrl = imageUrl;
};
}]);
My service.js file
'use strict';
/* Services */
var bookServices = angular.module('bookServices', ['ngResource']);
bookServices.factory('Book', ['$resource',
function($resource){
return $resource('https://api.myjson.com/bins/1oofv', {}, {
query: {method:'GET', params:{bookId:'bookId'}, isArray:true}
});
}]);
So what is happening.
My SHOW file - which displays all books within my book.json file which I have hosted on myjson displays all the content I need.
It displays all thumbnails accordingly with the following html:
<div class="container">
<!--Nav Bar-->
<div class="row">
<div class="col-md-4">
Find me the best
<select ng-model="orderProp" class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>
</div>
<div class="col-md-4">
Books about
<select ng-model="orderProp" class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
<option ng-repeat="book in books" value="">{{ book.genre.name }}</option>
</select>
</div>
<div class="col-md-4">
<div class="input-group">
<input ng-model="query" type="text" class="form-control" placeholder="Search books">
<span class="input-group-btn">
<button class="btn btn-default" type="button"><span class="glyphicon glyphicon-search" ></span></button>
</span>
</div>
</div>
</div>
<!--Body content-->
<div class="row">
<div class="col-md-4" ng-repeat="book in books | filter:query | orderBy:orderProp">
<div class="thumbnail" id="books">
<img ng-src="{{ book.cover }}">
<div class="caption">
<h4>{{ book.name }}</h4>
<h5>{{ book.author.name }}</h5>
<span class="glyphicon glyphicon-heart likes">{{ book.likes }}</span>
<span style="float: right;">{{ book.published | date:'yyyy-MM-dd' }}</span>
</div>
</div>
</div>
</div>
</div>
This is all fine - now when I click a specific book or author I am expecting the app to nav to the next page a detail ALL the details I need in the following template (but it doesn't):
<div class="container">
<div class="row">
<header>
<h1></h1>
<h4></h4>
<p></p>
<h6>Categories:</h6>
<span>{{ book.genre.category }}</span>
</header>
</div>
<div class="row">
<h3>Introduction</h3>
<h1></h1>
<p></p>
<div class="line-sep"></div>
<img ng-src="{{ avatar }}" class="img-round" />
<h2></h2>
</div>
<div class="row">
<h1>Similar reading</h1>
<p></p>
<div class="col-md-4">
<div class="thumbnail" id="books">
<img ng-src="http://placehold.it/350x500">
<div class="caption">
<h4>Lorem Ipsum Dolar Asar</h4>
<h5>By Mr Lorem Ipsum</h5>
<span class="glyphicon glyphicon-heart likes">1032</span>
<span style="float: right;">3 days ago</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="thumbnail" id="books">
<img ng-src="http://placehold.it/350x500">
<div class="caption">
<h4>Lorem Ipsum Dolar Asar</h4>
<h5>By Mr Lorem Ipsum</h5>
<span class="glyphicon glyphicon-heart likes">1032</span>
<span style="float: right;">3 days ago</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="thumbnail" id="books">
<img ng-src="http://placehold.it/350x500">
<div class="caption">
<h4>Lorem Ipsum Dolar Asar</h4>
<h5>By Mr Lorem Ipsum</h5>
<span class="glyphicon glyphicon-heart likes">1032</span>
<span style="float: right;">3 days ago</span>
</div>
</div>
</div>
</div>
</div>
Look at my josn file on the site: https://api.myjson.com/bins/1oofv
All I need to do is the following:
Use the json file to display the books (with some details) on the list page - in whatever display style I feel is appropriate.
Then - once all the books are displayed (with search and filter abilities) - be able to click on a book and be taken to a BOOK DETAIL page where we will delve into more detail about the book and show books that are similar to them.

stop stressing, and follow the Flatlander Gemstore tutorial.
First problem I could spot is, you use Book.get but defined Book.query
Secondly, using href="#" breaks angular, either use an <a href="">or, as I prefer, simply use a button, a hyperlink that goes nowhere isn't a link.
as yeouuu said, href="{{ book.id }}" seems wrong, this is either a semantical error and should be href="{{ book.url }}" or this is simply an error and should have been something else entirely.
Do the tutorial, it is a great resource.

Related

how can fix data not showing with angularjs and laravel

Is anyone help me, when I start to code to show data in blade with angularJS, data not showing in the page, even the data exists in the console. This following code in my app.js
// app.js
$scope.view_tab = 'shop1';
$scope.changeTab = function(tab) {
$scope.view_tab = tab;
}
// List product
$scope.loadProduct = function () {
$http.get('/getproduct').then(function success(e) {
console.log(e.data);
$scope.products = e.data.product;
$scope.totalProduct = $scope.products.length;
$scope.currentPage = 1;
$scope.pageSize = 9;
$scope.sortKey = 'id_kain';
};
//index.blade.php
<div class="shop-top-bar">
<div class="shop-tab nav">
<a ng-class="{'active': view_tab == 'shop1'}" ng-click="changeTab('shop1')" data-toggle="tab">
<i class="fa fa-table"></i>
</a>
<a ng-class="{'active': view_tab == 'shop2'}" ng-click="changeTab('shop2')" data-toggle="tab">
<i class="fa fa-list-ul"></i>
</a>
</div>
</div>
<div class="shop-bottom-area mt-35">
<div class="tab-content jump">
<div class="tab-pane" ng-class="{active: view_tab == 'shop1'}">
<div class="row">
<div ng-repeat="data in filtered = ( products | filter:search ) | orderBy:sortData | itemsPerPage: 9" class="col-xl-4 col-md-6 col-lg-6 col-sm-6">
<div class="product-wrap mb-25 scroll-zoom">
<div class="product-img">
<a ng-click="showForm(data)">
<img class="default-img" ng-src="#{{ data.gambar_kain[0].gambar_kain }}" alt="">
<img class="hover-img" ng-src="#{{ data.gambar_kain[1].gambar_kain }}" alt="">
</a>
</div>
<div class="product-content text-center">
<h3>#{{data.nama_kain}}</h3>
<div class="product-price">
<span>#{{numberingFormat(data.data_kain[0].harga_kain)}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane" ng-class="{active: view_tab == 'shop2'}">
<div dir-paginate="datas in filtered = ( products | filter:search ) | orderBy:sortData | itemsPerPage:9" class="shop-list-wrap mb-30">
<div class="row">
<div class="col-xl-4 col-lg-5 col-md-5 col-sm-6">
<div class="product-wrap">
<div class="product-img">
<a ng-click="showForm(datas)">
<img class="default-img" ng-src="#{{ datas.gambar_kain[0].gambar_kain }}" alt="">
<img class="hover-img" ng-src="#{{ datas.gambar_kain[1].gambar_kain }}" alt="">
</a>
</div>
</div>
</div>
<div class="col-xl-8 col-lg-7 col-md-7 col-sm-6">
<div class="shop-list-content">
<h3>#{{data.nama_kain}}</h3>
<div class="product-list-price">
<span>#{{numberingFormat(datas.data_kain[0].harga_kain)}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="pro-pagination-style text-center mt-30">
<dir-pagination-controls
max-size="1"
direction-links="true"
boundary-links="true" >
</dir-pagination-controls>
</div>
</div>
Data still not shows in the page, there are not errors in this page, but I don't know how can I fix this.
As I know there is two way to show data in Angular JS when you define the scopes then you need the ng-bind or ng-model to show data in the front end.
1- AngularJS Data Binding
live Example https://www.w3schools.com/code/tryit.asp?filename=G2DQQ2GSJ3EO
<div ng-app="myApp" ng-controller="myCtrl">
<p ng-bind="firstname "></p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John will be change";
});
</script>
2-AngularJS ng-model Directive
Live Example https://www.w3schools.com/code/tryit.asp?filename=G2DQQEUEPSOC
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
NOTE: try with static data and then check that you have valid data to make it dynamic
I hope this helps you!

$scope variable not rendering in view

I am getting started with angularjs and started working on a simple example. Overall I didn't ran into big challenges so far but this one i couldn't figure out... I have some $scope variables defined like this:
'use strict';
angular.module('portalApp')
.controller('summaryController', ['$scope', function ($scope) {
$scope.todayProduction = '8';
$scope.yesterdayProduction = '20';
$scope.monthlyProduction = '500';
$scope.dockedVessels = '16';
}]);
and this is my view html
<div class="row">
<div class="col-md-3 col-sm-6">
<div class="panel panel-default">
<div class="panel-body">
<div>
<span class="glyphicon glyphicon-stats" style="font-size: 50px;float: right;"></span>
<div>Today's Production</div>
<div style="font-size: 24px;">{{todayProduction}}<span style="font-size: 12px;"> tons</span></div>
</div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="panel panel-default">
<div class="panel-body">
<div>
<span class="glyphicon glyphicon-stats" style="font-size: 50px;float: right;"></span>
<div>Yesterdays's Production</div>
<div style="font-size: 24px;">{{yesterdayProduction}}<span style="font-size: 12px;"> tons</span></div>
</div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="panel panel-default">
<div class="panel-body">
<div>
<span class="glyphicon glyphicon-stats" style="font-size: 50px;float: right;"></span>
<div>Monthly Production</div>
<div style="font-size: 24px;">{{monthlyProduction}}<span style="font-size: 12px;"> tons</span></div>
</div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="panel panel-default">
<div class="panel-body">
<div>
<span class="glyphicon glyphicon-stats" style="font-size: 50px;float: right;"></span>
<div>Docked Vessels</div>
<div style="font-size: 24px;">{{dockedVessels}}<span style="font-size: 12px;"> tons</span></div>
</div>
</div>
</div>
</div>
</div>
The problem is that when rendering I see the glyphicon as expected but instead of displaying the variable values I get "{{variableName}}" for all of them.
As a background the sample project I am using was originally for 1.5 version but I'm running a 1.6
Thanks in advance for your help!!
Change {{monthlyProduc tion}} to {{monthlyProduction}} in your html.

AngularJS GitHub API| Conributors list in the repository list

I have a problem with the repository list in the list of all Conributors for this repository.
I want to create a Contributors list in the list of repositories downloaded using GitHub Api. However, I can not get these data for each repository and put them in html.
Has anyone any idea how to do this?
Thank you in advance for your help
My code:
App in html
<div ng-controller="gitApi" ng-app="app">
<div class="container">
<h1 class="text-center">
<span ng-hide="userData" />Loading</span>
<span />{{userData.name}}</span>
<br>
<a href="{{userData.html_url}}" class="btn btn-default">
{{userData.login}}
</a>
</h1>
<div class="panel panel-default">
<div class="panel-heading">
<form class="form-inline">
<span>
<h4>Repos <span class="badge">{{repoData.length}}</span>
<input ng-model="searchText" placeholder="Search" class="form-control input-sm">
</h4>
</span>
</form>
</div>
<div class="panel-body">
<div class="list-group">
<div ng-repeat="orgs in orgsData | filter:searchText | orderBy:predicate:reverse" class="list-group-item ">
<div class="row">
<div class="col-md-6">
<h4>
<a href="{{repo.html_url}}" target="_blank">
{{orgs.name}}
</a>
<small>{{orgs.description}}</small>
</h4>
<small>
<a href="{{orgs.homepage}}" class="">
<i class="fa fa-link"></i> WebPage
</a>
</small>
</div>
<div class="col-md-6">
Conributors List:
<div ng-repeat=" | filter:searchText | orderBy:predicate:reverse" class="list-group-item ">
<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
APP.js
angular.module('app', [])
.controller('gitApi', ['$scope', '$http', function($scope, $http) {
$scope.reposLoaded = false;
$scope.userLoaded = false;
$scope.orgsLoaded = false;
$http.get("https://api.github.com/users/angular")
.success(function(data) {
$scope.userData = data;
loadOrgsRepos();
});
var loadOrgsRepos = function() {
$http.get("https://api.github.com/orgs/angular/repos")
.success(function(data) {
$scope.orgsData = data;
});
}
$scope.predicate = '-updated_at';
}]);
You can get all contributors url from contributors_url and make an API call for each one of these, storing the result in the original $scope.orgsData array :
"use strict";
var githubApp = angular.module('app', []);
githubApp.controller('gitApi', ['$scope', '$http', '$q', function($scope, $http, $q) {
$http.get("https://api.github.com/users/angular")
.success(function(data) {
$scope.userData = data;
loadOrgsRepos();
});
var loadOrgsRepos = function() {
$http.get("https://api.github.com/orgs/angular/repos")
.success(function(data) {
$scope.orgsData = data;
var contribs = [];
for (var i in data) {
contribs.push(data[i].contributors_url);
}
$q.all(contribs.map(function(item) {
return $http({
method: 'GET',
url: item
});
}))
.then(function(results) {
results.forEach(function(val, i) {
$scope.orgsData[i].contributors = val.data;
});
});
});
}
$scope.repo_sort = '-updated_at';
$scope.contrib_sort = '-contributions'
}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body id="page-top" class="index">
<div ng-controller="gitApi" ng-app="app">
<div class="container">
<h1 class="text-center">
<span ng-hide="userData">Loading</span>
<span>{{userData.name}}</span>
<br>
<a href="{{userData.html_url}}" class="btn btn-default">
{{userData.login}}
</a>
</h1>
<div class="panel panel-default">
<div class="panel-heading">
<form class="form-inline">
<span>
<h4>Repos <span class="badge">{{repoData.length}}</span>
<input ng-model="searchText" placeholder="Search" class="form-control input-sm">
</h4>
</span>
</form>
</div>
<div class="panel-body">
<div class="list-group">
<div ng-repeat="orgs in orgsData | filter:searchText | orderBy:repo_sort:reverse" class="list-group-item ">
<div class="row">
<div class="col-md-8">
<h4>
<a href="{{repo.html_url}}" target="_blank">
{{orgs.name}}
</a>
<small>{{orgs.description}}</small>
</h4>
<small>
<a href="{{orgs.homepage}}" class="">
<i class="fa fa-link"></i> WebPage
</a>
</small>
</div>
<div class="col-md-6">
Conributors List:
<div class="list-group-item">
<div class="row">
<div class="col-md-4">
name
</div>
<div class="col-md-4">
avatar
</div>
<div class="col-md-4">
contributions
</div>
</div>
</div>
<div ng-repeat="contrib in orgs.contributors | filter:searchText | orderBy:contrib_sort:reverse" class="list-group-item">
<div class="row">
<div class="col-md-4">
<a href="{{contrib.html_url}}" target="_blank">
{{contrib.login}}
</a>
</div>
<div class="col-md-4">
<img ng-src="{{contrib.avatar_url}}" height="42" width="42" />
</div>
<div class="col-md-4">
<p>
{{contrib.contributions}}
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
Here is another fiddle

Ionic / AngularJS key not accessible in ng-repeat

I am trying to access the {{key}} from an ng-repeat from a controller using Ionic 1. The {{key}} that is generated as text is different from the {{key}} I am receiving at the controller, not sure why this is.
What I want to do is find the div id which should be dynamically generated using the id = "historyHeader_{{key}}" and need to receive it using ng-click="loadInlineHistory({{key}})"
Controller:
$scope.loadInlineHistory = function (dateLoaded) {
var textS = $("#historyHeader_"+dateLoaded).text();
}
View
<ion-list>
<div ng-repeat="(key, value) in history | groupBy: 'date'"" style="background:#fff;padding:0.4em">
<div class="item item-divider">
<span style="min-height: 100%" id="historyHeader_{{key}}"> {{key}}</span>
</div>
<div style="width: 100%">
<div class="row" ng-repeat="hist in value">
<div ng-click="testRepeatx()">
<div class="row">
<div class="col col-50" align="left">3 laterals </div>
<div class="col col-50" align="left" style="color:#999">{{hist.reps}} calories</div>
</div>
</div>
</div>
<div class="row" >
<button class="button button-block button-balanced" ng-click="loadInlineHistory({{key}})"> <i class="ion-plus"></i></button>
</div>
</div>
</div>
</ion-list>
Just remove the curly brackets from the ng-click function call.
<div class="row" >
<button class="button button-block button-balanced"
ng-click="loadInlineHistory(key)"> <i class="ion-plus"></i></button>
</div>

Laravel - must be of the type array, none given

Having a strange problem with my laravel 5.2 build, I'm trying to send data from a form to my database and for some reason an error keeps being thrown on submit which states "Argument 1 passed to App\Http\Controllers\PostsController::postStatus() must be of the type array, none given" and I'm not sure why as I am sending data into an array. My code is shown below.
timeline.blade.php
#extends('layouts.app')
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
<div class="row notifications-section">
<div class="col-md-4">
<div class="dropdown">
<a class="dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fa fa-caret-square-o-down"></i></a>
<ul class="dropdown-menu">
<li>option 1</li>
</ul>
</div>
</div>
<div class="col-md-4">
<div class="dropdown">
<a class="dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fa fa-comments"></i></a>
<ul class="dropdown-menu">
<li>option 1</li>
</ul>
</div>
</div>
<div class="col-md-4">
<div class="dropdown">
<a class="dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fa fa-users"></i></a>
<ul class="dropdown-menu">
<li>option 1</li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<form role="form" method="POST" action="{{ url('/home') }}" class="facebook-share-box">
{!! csrf_field() !!}
<div class="share">
<div class="panel panel-default">
<div class="panel-body">
<div class="">
<input type="hidden" name="user_name" value="{{ Auth::user()->firstName }} {{ Auth::user()->lastName }}">
<textarea name="body" cols="40" rows="10" id="status_message" value="{{ old('body') }}" class="form-control message" style="height: 62px; overflow: hidden;" placeholder="What's on your mind ?"></textarea>
</div>
<hr>
<div class="row">
<div class="col-md-7">
<div class="form-group">
<div class="btn-group">
<button type="button" class="btn btn-default"><i class="icon icon-map-marker"></i> Location</button>
<button type="button" class="btn btn-default"><i class="icon icon-picture" ></i> Photo</button>
</div>
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<div class="row">
<div class="col-md-8">
<select name="visibility" class="form-control privacy-dropdown pull-left input-sm" value="{{ old('visibility') }}">
<option value="1" selected="selected">Public</option>
<option value="2">Only my friends</option>
<option value="3">Only me</option>
</select>
</div>
<div class="col-md-3">
<input type="submit" name="submit" class="btn btn-primary btn-small">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="col-md-4 profile-section">
<div class="row">
<div class="col-md-8">
<h2>{{ Auth::user()->firstName }} {{ Auth::user()->lastName }}</h2>
<h4>{{ Auth::user()->currentLocation }}</h4>
<p>{{ Auth::user()->bio }}</p>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-2 connect-section">
<div class="row">
<div class="col-md-4">Section</div>
<div class="col-md-4">Section</div>
<div class="col-md-4">Section</div>
</div>
</div>
<div class="col-md-9 posts-section">
<div class="row">
#foreach($posts as $post)
<div class="col-md-4">
<h2>{{ $post->user_name }}</h2>
{{ $post->created_at }}<br />
{{ $post->body }}
{{ $post->visibility }}<br />
<div class="row like_comment_share">
<div class="col-md-4">Like</div>
<div class="col-md-4">Comment</div>
<div class="col-md-4">Share</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
#endsection
PostController.php
<?php
namespace App\Http\Controllers;
use App\Posts;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controller as BaseController;
class PostsController extends BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
}
public function display()
{
return view('users/timeline')
->with('user_name', 'body', 'photo', 'visibility', 'created_at')
->with('posts', Posts::all());
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
protected function postStatus(array $data)
{
return Posts::create([
'user_name' => $data['user_name'],
'body' => $data['body'],
'photo' => $data['photo'],
'visibility' => $data['visibility'],
]);
}
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $fillable = [
'user_name', 'body', 'photo', 'visibility', 'created_at',
];
}
No data is passed to the controller when you post to a route. Instead, you access the data through the Request object by injecting it into the method, or by using the Request:: or Input:: facades.
protected function postStatus(Illuminate\Http\Request $request)
{
$data = $request->all();
return Posts::create([
'user_name' => $data['user_name'],
'body' => $data['body'],
'photo' => $data['photo'],
'visibility' => $data['visibility'],
]);
}

Resources