Json array details show in three pages in angularjs with ionic - javascript

I have category array. there are more products.
I need show categories in category page.
when click on a category, I have to redirect the product page and show the necessary products.
When click on a product, I have to redirect the product_details page and show the necessary product details.
category loaded to the category page, when click on that it will redirect the product page. But, I cant see products.
And what are the errors of these controllers, and how to create "product_details.html", and "product.html" page also.
I have to show like this :
category page : term_id, name
product page : post_title, ID
product details page : post_title, post_date, post_author, ID
categorylist-product.json
{
"category": [{
"term_id": "10",
"name": "Arden Grange",
"slug": "arden-grange",
"products": [{
"ID": "47",
"post_title": "Arden Grange, Premium",
"post_date": "2015-10-20 16:13:04",
"post_author": "5"
}, {
"ID": "50",
"post_title": "Arden Grange Puppy\/Junior Large Breed",
"post_date": "2015-10-21 04:56:23",
"post_author": "5"
}, {
"ID": "53",
"post_title": "Arden Grange Weaning\/Puppy",
"post_date": "2015-10-22 12:52:35",
"post_author": "5"
}]
}, {
"term_id": "8",
"name": "Menu 1",
"slug": "menu-1",
"products": [{
"ID": "38",
"post_title": "Home",
"post_date": "2015-10-20 10:43:44",
"post_author": "1"
}, {
"ID": "30",
"post_title": "",
"post_date": "2015-10-20 10:13:56",
"post_author": "1"
}, {
"ID": "31",
"post_title": "",
"post_date": "2015-10-20 10:13:57",
"post_author": "1"
}]
}]
}
CategoryController.js
app.controller('CategoryController', ['$scope','category', function($scope, category) {
category.success(function(data) {
$scope.userslists = data;
});
}]);
ProductController.js
app.controller('ProductController', ['$scope', '$routeParams', 'category', function($scope, $routeParams, category,products) {
category.success(function(data) {
$scope.users = data.category[$routeParams.id];
});
}]);
app.js
var app = angular.module('LookApp', ['ionic','ngCordova','ngRoute']);
app.config(function($stateProvider, $urlRouterProvider,$routeProvider) {
$routeProvider
.when('/', {
controller: 'CategoryController',
templateUrl: 'views/category.html'
})
.when('/:id', {
controller: 'ProductController',
templateUrl: 'views/users.html'
})
.when('/login/:friendId', {
controller: 'LoginController',
templateUrl: 'views/login.html'
})
.otherwise({
redirectTo: '/'
});
category.js (Service file)
app.factory('category', ['$http', function($http) {
return $http.get('http://localhost/youtubewebservice/shop-categorylist-product.php')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);

see you have put your data in $scope.userlists so when you call your data in html then you need to do like this
category page
<div ng-repeat="user in userslists">
Term ID : {{user.category.term_id}}
Name : {{user.category.name}}
</div>
product page
<div ng-repeat="user in userslists">
Post Title : {{user.category.products.post_title}}
ID : {{user.category.products.ID}}
</div>
Details page
<div ng-repeat="user in userslists">
Post Title : {{user.category.products.post_title}}
Post Date : {{user.category.products.post_date}}
Post Author: {{user.category.products.post_author}}
ID : {{user.category.products.ID}}
</div>

Related

AngularJS $http.get not pulling back

Why is this not pulling back anything? I keep getting the error message and I cannot figure out why. I tried to use JSONParse on the response data and reassigning it to the $scope but that didn't help. I have no errors in my F12 Console. I know JQuery is a much better solution but I prefer the Javascript, i'll take both though. Thanks ahead of time.
var controlSeletor = angular.module('moduleName', []);
controlSeletor .controller('variableController', function($scope, $http) {
$http.get("data.json")
.then(
function(response) {
// Success
$scope.variable = response.data;
},
function(response){
// Error
alert("Error!");
}
);
});
The json is a separate file and looks like this:
[{
"url": "place.html",
"name": "Bob",
"pic": "bob.jpg"
},
{
"url": "place.html",
"name": "Mike",
"pic": "mike.jpg"
},
{
"url": "place.html",
"name": "Tony",
"pic": "tony.jpg"
}];
Your JSON is not valid, change it as follows,
[ {
"url": "place.html",
"name": "Bob",
"pic": "bob.jpg" }, {
"url": "place.html",
"name": "Mike",
"pic": "mike.jpg" }, {
"url": "place.html",
"name": "Tony",
"pic": "tony.jpg" } ]
var app = angular.module('filterApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get('data.json').
then(function(response) {
$scope.variable = response.data;
});
});
DEMO
Few observations :
Controller declaration is not correct. It should be controlSeletor.controller instead of controlSeletor .controller.
Your JSON is not a valid JSON.
Valid JSON should be like this :

Refreshing Angular App takes to the default state

I am creating angularjs based Dashboeard, I am using ui.router to create states from json file,
My topNavbar and states are being created by pages.json file.
My app works fine but when refresh the page with this"localhost:#/map" url it redirects to "localhost:#/dashboard" state.
how I can set the state which user enters in URL?
var $urlRouterProviderRef = null;
var $stateProviderRef = null;
myApp.run(function ($q, $rootScope, $state, $http, navbars)
{
navbars.load()
$http.get("js/config/pages.json")
.success(function (data) {
angular.forEach(data, function (value, key) {
var state = {
"url": value.url,
"templateUrl": value.templateUrl,
"controller": value.controller
};
$stateProviderRef.state(value.sref, state);
});
});
}
myApp.config(function ($stateProvider, $locationProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/dashboard');
$urlRouterProviderRef = $urlRouterProvider;
$stateProviderRef = $stateProvider;
})
pages.json
[
{
"displayname": "Dashboard",
"active": true,
"name": "dashboard",
"sref": "dashboard",
"url": "/dashboard",
"controller": "layoutOneCtrl",
"templateUrl": "views/dashboard.html",
"icon": "fa-dashboard",
"children": [{
"name": "dashboard"
}]
}, {
"displayname": "Map",
"open": false,
"active": false,
"name": "map",
"sref": "map",
"url": "/map",
"templateUrl": "views/layout1.html",
"controller": "layoutOneCtrl",
"icon": "fa-map-marker",
"children": [{
"name": "map"
}]
}
.
.
.
]
when you refresh page , your run function called and read value from pages.json , you need to update pages.json file or you need to save the url value in localstorage or sessionstorage so whenever you refresh the page you can access url value from localstorage or sessionstorage.

ionic ui-router resolve's result didn't show in the page

Here is the code
APP.js
.state('app.aboutus', {
url: '/aboutus',
views: {
'mainContent': {
templateUrl: 'templates/aboutus.html',
controller: 'AboutController',
resolve: {
leaders: ['corporateFactory', function(corporateFactory){
return corporateFactory.query();
}]
}
}
}
})
Controller.js
.controller('AboutController', ['$scope', 'leaders', 'baseURL', function($scope, leaders, baseURL) {
$scope.baseURL = baseURL;
$scope.showLeaders = false;
$scope.message="Loading ...";
$scope.leaders = leaders;
console.log($scope.leaders);
}])
Aboutus.html
<ion-view view-title="About Us">
<ion-content>
<div class="card">
<div class="list list-inset">
<div ng-repeat="leaders in leadership" class="item item-avatar item-text-wrap">
<h2>{{leaders.name}}</h2>
<h4>{{leaders.designation}}</h4>
</div>
</div>
</div>
</ion-content>
</ion-view>
db.json
"leadership": [
{
"id": 0,
"name": "Human 1",
"designation": "Chief Executive Officer"
},
{
"id": 1,
"name": "Human 2",
"designation": "Chief Food Officer"
},
{
"id": 2,
"name": "Human 3",
"designation": "Chief Taste Officer"
},
{
"id": 3,
"name": "Human 4",
"designation": "Executive Chef"
}
]
When I checked using inspect element - console tab in Google Chrome, the $resolved: has successfully changed from false into true, and every array contain the information per ID.
But, the data didn't show in the aboutus.html
Did I typed something wrong? or maybe I'm using a wrong method?
Thank you in advance!
Your problem is likely that your ng-repeat syntax is incorrect. Where you have leadership, that should be the object/array you're referencing in your scope. So try changing it to this:
<div ng-repeat="leader in leaders" class="item item-avatar item-text-wrap">
<h2>{{leader.name}}</h2>
<h4>{{leader.designation}}</h4>
</div>

Angular translate and ui router

I'm trying to translate a multistep form with angular translate and for routing, i'm using ui-router .
everything works fine except one thing .
here is my code :
translate
.config(function ($translateProvider) {
$translateProvider.useStaticFilesLoader({
prefix: 'App/i18n/locale-',
suffix: '.json'
});
$translateProvider.preferredLanguage('ir');
})
en.json and ir.json
{
"wizardForm":{
"stepOne":{
"LABEL": "ثبت متقاضی",
"NATIONALCODE": "کد ملی",
"NAME": "نام",
"FATHERNAME": "نام پدر",
"GENDER": "جنسیت",
}
}
en.json////////
{
"wizardForm":{
"stepOne":{
"LABEL": "Register Requester",
"NATIONALCODE": "National Code",
"NAME": "NAme",
"FATHERNAME": "Father Name",
"GENDER": "Gender",
}
}
html
<label translate="wizardForm.stepOne.NATIONALCODE">
</label>
translate works fine in other sections but not in form ? am i missing something ?
Your json is not valid. It should be
{
"wizardForm": {
"stepOne": {
"LABEL": "Register Requester",
"NATIONALCODE": "National Code",
"NAME": "NAme",
"FATHERNAME": "Father Name",
"GENDER": "Gender"
}
}
}
and not
{
"wizardForm":{
"stepOne":{
"LABEL": "Register Requester",
"NATIONALCODE": "National Code",
"NAME": "NAme",
"FATHERNAME": "Father Name",
"GENDER": "Gender",
}
}
You are missing one '}' and have one ',' too much.
Try your json here: jsonlint
Does that help?

ng-repeat on a multi-dimensional array/object

I am getting json object from an external url.
$http.get('http://dev-newsbackend.gotpantheon.com/articles')
.success(function(data, status){
$scope.articles = data.articles;
console.log(data.articles);
}).error(function(err){
console.log(err);
})
})
The json I get is as below:
{
"articles": [
{
"single": {
"title": "7 Tech Upgrades to the Old-School Science Class"
}
},
{
"single": {
"title": "What Will the ALS Association Do With That $100 Million?"
}
},
{
"single": {
"title": "President Obama Rocked a Tan Suit, So Twitter Went Nuts"
}
}
],
"view": {
"name": "articles",
"display": "page",
"path": "articles",
"root": "articles",
"child": "single",
"pages": 1,
"page": 0,
"count": 3,
"limit": 10
}
}
However, when I am doing a ng-repeat though the element is printed multiple times. Am not getting the value.
<ion-list ng-repeat="article in articles">
<ion-item>Test : {{ single.title }}</ion-item>
</ion-list>
My developer console screen-shot:
This is what I got for console.log(data.articles);
The screen-shot of my browser where am getting the list without the title value:
Try this:
<ion-list ng-repeat="article in articles">
<ion-item>Test : {{article.single.title }}</ion-item>
</ion-list>

Categories