Related
Is there a nice way in Angular's ngRepeat to pull a value out of a JSON Array depending on its key. I working with the following JSON structure and would like to output the associated value of "Name 2" for each object.
{
"Items": [
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
}
try like this
angular.forEach(data.Items,function(value,key){
angular.forEach(value,function(v){
if(v.name == "Name 2")
console.log(v);
}
})
Here is an example. It will print the Value for "Name 2":
<body ng-app="myApp">
<div ng-controller="myCtrl">
<div ng-repeat="item in records.Items">
<div ng-repeat="singleAttribute in item.Attributes">
<div ng-if="singleAttribute.Name === 'Name 2'">
{{singleAttribute.Value}}
</div>
</div>
</div>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = {
"Items": [
{
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
};
});
</script>
</body>
Here is mij code but data is hardcoded and working perfect. When I try my data dynamicly calling data responsed good. My question is look this script with hardcoded data. If i will selected item viewing in detailpage working nice.
Shortly this hardcoded data must from webmethod. How can I do it?
This is my html code for view menu as list;
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li class="active">
<i class="fa fa-dashboard"></i> Menu
</li>
</ol>
<div class="row" ng-controller="DesktopController">
<div class="col-lg-10">
<h2>Menu Lijst</h2>
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Description</th>
<th>State</th>
<th>ParentID</th>
<th>Parent</th>
<th>MenuItem ID</th>
<th>Index</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="menu in List" style="width:auto;">
<td>{{menu.Description}}</td>
<td>{{menu.State}}</td>
<td>{{menu.ParentID}}</td>
<td>{{menu.Parent}}
</td>
<td>{{menu.MenuItemID}}</td>
<td>{{$index}}</td>
<td>
View Detail
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Here is my detail menu view page...
<h1>View DetailMenu Page</h1>
<br />
<!--<h1>{{menu.Description}}</h1>-->
<div class="row" ng-controller="viewDetailMenuController">
<div class="col-lg-10">
<h2>{{menu.Description}}</h2>
</div>
</div>
App.factory('menuService', function ($http) {
var obj = {
getAllMenus: [{ "ID": "1", "Description": "MOTOR", "State": "1", "ParentID": "0", "Parent": "", "MenuItemID": "0" }, { "ID": "2", "Description": "FILTERS", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "1" }, { "ID": "3", "Description": "BRANDSTOF/TANKDOPPEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "2" }, { "ID": "4", "Description": "ONSTEKING", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "3" }, { "ID": "5", "Description": "ACCU/STARTEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "4" }, { "ID": "6", "Description": "BOUGIES/BOUGIEKABELS", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "5" }, { "ID": "7", "Description": "D-RIEMEN/V-REIM-SETS", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "6" }, { "ID": "8", "Description": "PAKKINGEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "7" }, { "ID": "9", "Description": "MOTORBLOK", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "8" }, { "ID": "10", "Description": "CYLINDERKOP", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "9" }, { "ID": "11", "Description": "MOTORMANAGEMENT", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "10" }, { "ID": "12", "Description": "UITLATEN", "State": "0", "ParentID": "1", "Parent": "MOTOR", "MenuItemID": "11" }, { "ID": "13", "Description": "KOELING", "State": "2", "ParentID": "0", "Parent": "", "MenuItemID": "12" }, { "ID": "14", "Description": "AIRCO", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "13" }, { "ID": "15", "Description": "KOELSYSTEEM/WATERPOMP", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "14" }, { "ID": "16", "Description": "SCHAKELAARS/SENSOREN", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "15" }, { "ID": "17", "Description": "SLANGEN/LEIDINGEN", "State": "0", "ParentID": "13", "Parent": "KOELING", "MenuItemID": "16" }, { "ID": "18", "Description": "ONDERSTEL", "State": "1", "ParentID": "0", "Parent": "", "MenuItemID": "17" }, { "ID": "19", "Description": "STUURDELEN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "18" }, { "ID": "20", "Description": "KABELS", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "19" }, { "ID": "21", "Description": "AANDRIJVING/HOEAEN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "20" }, { "ID": "22", "Description": "KOPPELING", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "21" }, { "ID": "23", "Description": "WIELLAGERS", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "22" }, { "ID": "24", "Description": "SCHOKDEMPERS", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "23" }, { "ID": "25", "Description": "VEREN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "24" }, { "ID": "26", "Description": "TREKHAAK", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "25" }, { "ID": "27", "Description": "BANDEN", "State": "0", "ParentID": "19", "Parent": "STUURDELEN", "MenuItemID": "26" }, { "ID": "28", "Description": "REMDELEN NAT/DROOG", "State": "2", "ParentID": "0", "Parent": "", "MenuItemID": "27" }, { "ID": "29", "Description": "REM-DELEN/SCHIJVEN", "State": "0", "ParentID": "28", "Parent": "REMDELEN NAT/DROOG", "MenuItemID": "28" }]
}
return obj;
});
Controllers:
var App = angular.module("app", ['ui.router']);
App.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'Pages/Home.html'
})
.state('menu', {
url: '/menu',
templateUrl: 'Pages/Menu.html'
})
.state('menuDetail', {
url: '/menuDetail/{id}',
templateUrl: 'Pages/MenuDetail.html',
controller: 'viewDetailMenuController'
});
});
App.run(function ($rootScope, $state) {
$rootScope.$state = $state;
});
App.controller("DesktopController", function ($scope, menuService) {
$scope.List = menuService.getAllMenus;
});
App.controller("viewDetailMenuController", function ($scope, menuService, $stateParams) {
$scope.menu = menuService.getAllMenus[$stateParams.id];
});
This work perfect. Only my question is how can I do that selected menu view in detail page using $http $stateParams (dynamic data calling from webservice or webmethod)?
Before this question I have shared calling data from webmethod you can see it.
Thanks...
write this code
.state('menuDetail', {
url: '/menuDetail/:id',
templateUrl: 'Pages/MenuDetail.html',
controller: 'viewDetailMenuController'
});
<a ui-sref="menuDetail({ id: dynamicId })">Click</a>
and when you click on 'Click' then open detail page
For more know https://github.com/angular-ui/ui-router/wiki/quick-reference
Let's say you want to get your menu items from server instead of hardcoding.
App.factory('menuService', function ($http) {
var service = {};
service.getAllMenus = function(){
return $http.get('url_or_your_webapi');//this returns a promise
};
return service;
});
then we need to get that menu data in controller:
App.controller("DesktopController", function ($scope, menuService) {
// here we set callback for promise we received from our menu service
menuService.getAllMenus().then(function(response){$scope.List = response.data;});
});
UPD menu service code fixed, now returns function
My CakePHP application generates a JSON output of all my categories as shown below:
{
"categories": [
{
"Category": {
"id": "7",
"name": "Elektronics"
}
},
{
"Category": {
"id": "8",
"name": "Gym"
}
},
{
"Category": {
"id": "4",
"name": "Nightlife"
}
},
{
"Category": {
"id": "6",
"name": "Shopping "
}
},
{
"Category": {
"id": "2",
"name": "Sport"
}
}
]
}
How can I use this data with the ngTagsInput plugin? I tried it like this but it always shows an error. It seems like it cannot handle multiple objects.
<label class="item item-input item-stacked-label">
<span class="input-label">Categories</span>
<tags-input ng-model="tags" display-property="Category.name" placeholder="New Category">
<auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
</label>
Error:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: tag in tagList.items track by track(tag), Duplicate key: undefined, Duplicate value: {"Category":{"id":"7","name":"Electronics"}}
http://errors.angularjs.org/1.4.3/ngRepeat/dupes?p0=tag%20in%20tagList.item…2%3A%7B%22id%22%3A%225%22%2C%22name%22%3A%22Essen%20und%20Trinken%22%7D%7D
at ionic.bundle.js:8900
at ngRepeatAction (ionic.bundle.js:35974)
at Object.$watchCollectionAction [as fn] (ionic.bundle.js:24382)
at Scope.$digest (ionic.bundle.js:24515)
at Scope.$apply (ionic.bundle.js:24783)
at done (ionic.bundle.js:19196)
at completeRequest (ionic.bundle.js:19368)
at XMLHttpRequest.requestLoaded (ionic.bundle.js:19309)
UPDATE
You could create a function that converts your object in something that tags-input can understand.
var object = {
"categories": [
{
"Category": {
"id": "7",
"name": "Elektronics"
}
},
{
"Category": {
"id": "8",
"name": "Gym"
}
},
{
"Category": {
"id": "4",
"name": "Nightlife"
}
},
{
"Category": {
"id": "6",
"name": "Shopping "
}
},
{
"Category": {
"id": "2",
"name": "Sport"
}
}
]
};
var categories = [];
for(var i = 0; i < object.categories.length; i++){
var category = object.categories[i].Category;
var categoryToPush = {
id: category.id,
name: category.name
};
categories.push(categoryToPush);
}
categories will contain:
[{
"id": "7",
"name": "Elektronics"
}, {
"id": "8",
"name": "Gym"
}, {
"id": "4",
"name": "Nightlife"
}, {
"id": "6",
"name": "Shopping "
}, {
"id": "2",
"name": "Sport"
}]
So then you can use it in the directive writing:
<tags-input ng-model="tags" display-property="name">
<auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
OLD ANSWER
Add a key-property value:
<tags-input ng-model="tags" display-property="Category.name" key-property="Category.id" placeholder="New Category">
<auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
Similar issue.
So I made a webshop with firebase, and I want each product to have its own review section, and to keep it optimized I have to keep my data as flat as possible.
Consider this database structure
Lets say, after I repeat my 7 products. I want to repeat all their corresponding reviews in them, I'm new to angular & really can't figure it out.
Check out this jsfiddle which is an example of my data & lays down what I want to achieve.
// the main (app) module
var myApp = angular.module("myApp", []);
// add a controller
myApp.controller("showProducts", function($scope) {
//Products
$scope.products = [{
"brand": "Apple",
"id": 0,
"img": "img/macbook.png",
"model": 2015,
"name": "Macbook 13'",
"operatingSystem": "OS X",
"price": "1900"
}, {
"brand": "Google",
"id": 1,
"img": "img/chromebook.png",
"model": "Pixel 2",
"name": "Chromebook",
"operatingSystem": "Chrome OS",
"price": "1700"
}, {
"brand": "Dell",
"id": 2,
"img": "img/dellxps13.png",
"model": "XPS13",
"name": "Dell XP13",
"operatingSystem": "Win 10",
"price": "1500"
}, {
"brand": "Lenovo",
"id": 3,
"img": "img/lenovo.png",
"model": "A2",
"name": "Lenovo A2",
"operatingSystem": "Win 7",
"price": "999"
}, {
"brand": "Sony",
"id": 4,
"img": "img/vaio.png",
"model": "VAIO",
"name": "Sony vaio",
"operatingSystem": "Win 8.0",
"price": "1200"
}, {
"brand": "Acer",
"id": 5,
"img": "img/acer.png",
"model": "a7",
"name": "Acer A7",
"operatingSystem": "Win 8.1",
"price": "1199"
}, {
"brand": "AW",
"id": 6,
"img": "http://gearnuke.com/wp-content/uploads/2015/07/alienware-1-1.jpg",
"model": "5A",
"name": "Alienware",
"operatingSystem": "Windows 10",
"price": "5000"
}];
//Reviews
$scope.reviews = [{
"-K1bfGsnWOclf1R85Uec": {
"comment": "This laptop is great",
"rating": "5",
"user": "Maryam"
}
}, {
"-K1bg0wHIUQ9qbxspLmg": {
"comment": "This laptop is working good so far!",
"rating": "4",
"user": "spongbob"
},
"-K1bzqAQpr1br6h04bSH": {
"comment": "some comment",
"rating": "10",
"user": "test"
}
}, {
"-K1bfvS9MYGE82nTx67r": {
"comment": "This laptop is meh",
"rating": "3",
"user": "superman"
}
}, {
"-K1bgSTfbjQ-LRDScH-3": {
"comment": "daym son",
"rating": "10",
"user": "Omar"
}
}, {
"-K1bgljvaeY94E_aPQvY": {
"comment": "commenthere",
"rating": "10",
"user": "pewdiepie"
}
}, {
"-K1bgmXWai9PDake2Wh1": {
"comment": "commenthere",
"rating": "10",
"user": "tiesto"
}
}, {
"-K1bgnMlsp_Dl5ooBl2G": {
"comment": "commenthere",
"rating": "10",
"user": "ex-gf"
},
"-K1gxHCtPdEniOGNFcWi": {
"comment": "idk",
"rating": 3,
"user": "idk"
}
}];
});
.card {
margin: 1% 1% 1% 1%;
padding: 1% 1% 1% 1%;
width: 250px;
height: 150px;
overflow-y: auto;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="showProducts">
<div class="card" ng-repeat="product in products">
<h5>{{ product.name }}</h5>
</div>
<div ng-repeat="review in reviews">
<div ng-repeat="(key, data) in review">User:{{ data.user }} | Comment:{{ data.comment }} | Rating:{{ data.rating }}</div>
</div>
</body>
At the bottom of my results, you will see all the comments. I have tried many things to get them in the right places without much success.
You can use $index to grab the correct list of reviews per product. You'll only need two ng-repeat.
// the main (app) module
var myApp = angular.module("myApp", []);
// add a controller
myApp.controller("showProducts", function($scope) {
//Products
$scope.products = [{
"brand": "Apple",
"id": 0,
"img": "img/macbook.png",
"model": 2015,
"name": "Macbook 13'",
"operatingSystem": "OS X",
"price": "1900"
}, {
"brand": "Google",
"id": 1,
"img": "img/chromebook.png",
"model": "Pixel 2",
"name": "Chromebook",
"operatingSystem": "Chrome OS",
"price": "1700"
}, {
"brand": "Dell",
"id": 2,
"img": "img/dellxps13.png",
"model": "XPS13",
"name": "Dell XP13",
"operatingSystem": "Win 10",
"price": "1500"
}, {
"brand": "Lenovo",
"id": 3,
"img": "img/lenovo.png",
"model": "A2",
"name": "Lenovo A2",
"operatingSystem": "Win 7",
"price": "999"
}, {
"brand": "Sony",
"id": 4,
"img": "img/vaio.png",
"model": "VAIO",
"name": "Sony vaio",
"operatingSystem": "Win 8.0",
"price": "1200"
}, {
"brand": "Acer",
"id": 5,
"img": "img/acer.png",
"model": "a7",
"name": "Acer A7",
"operatingSystem": "Win 8.1",
"price": "1199"
}, {
"brand": "AW",
"id": 6,
"img": "http://gearnuke.com/wp-content/uploads/2015/07/alienware-1-1.jpg",
"model": "5A",
"name": "Alienware",
"operatingSystem": "Windows 10",
"price": "5000"
}];
//Reviews
$scope.reviews = [{
"-K1bfGsnWOclf1R85Uec": {
"comment": "This laptop is great",
"rating": "5",
"user": "Maryam"
}
}, {
"-K1bg0wHIUQ9qbxspLmg": {
"comment": "This laptop is working good so far!",
"rating": "4",
"user": "spongbob"
},
"-K1bzqAQpr1br6h04bSH": {
"comment": "some comment",
"rating": "10",
"user": "test"
}
}, {
"-K1bfvS9MYGE82nTx67r": {
"comment": "This laptop is meh",
"rating": "3",
"user": "superman"
}
}, {
"-K1bgSTfbjQ-LRDScH-3": {
"comment": "daym son",
"rating": "10",
"user": "Omar"
}
}, {
"-K1bgljvaeY94E_aPQvY": {
"comment": "commenthere",
"rating": "10",
"user": "pewdiepie"
}
}, {
"-K1bgmXWai9PDake2Wh1": {
"comment": "commenthere",
"rating": "10",
"user": "tiesto"
}
}, {
"-K1bgnMlsp_Dl5ooBl2G": {
"comment": "commenthere",
"rating": "10",
"user": "ex-gf"
},
"-K1gxHCtPdEniOGNFcWi": {
"comment": "idk",
"rating": 3,
"user": "idk"
}
}];
});
.card {
margin: 1% 1% 1% 1%;
padding: 1% 1% 1% 1%;
width: 250px;
height: 150px;
overflow-y: auto;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="showProducts">
<div class="card" ng-repeat="product in products">
<h5>{{ product.name }}</h5>
<div ng-repeat="(key, data) in reviews[$index]">User:{{ data.user }} | Comment:{{ data.comment }} | Rating:{{ data.rating }}</div>
</div>
</body>
I want to repeat all their corresponding reviews
There must be a relationship between your products and their reviews. So try to add product id to the reviews for mapping.
Once you have a mapping between product and their respective reviews you can for something like below with filter to achieve your functionality.
<div ng-repeat="product in products">
<div class="card"><h5>{{ product.name }}</h5></div>
<div ng-repeat="review in reviews | filter:{productId: product.id}"">
<div ng-repeat="(key, data) in review">User:{{ data.user }} | Comment:{{ data.comment }} | Rating:{{ data.rating }}</div>
</div>
</div>
I am using syncfusion dropdownlist. I want to create a dropdownlist for blood group in my form. How can I do that in angularjs?
<input type="text" ej-dropdownlist e-datasource="BloodGroup_List" e-value="Name" ng-model="employee.bloodgroup">
In my controller I have written,
$scope.bloodgroup = [{ "Id": "1", "Name": "O+" },
{ "Id": "2", "Name": "O-" },
{ "Id": "3", "Name": "A+" },
{ "Id": "4", "Name": "A-" },
{ "Id": "5", "Name": "B+" },
{ "Id": "6", "Name": "B-" },
{ "Id": "7", "Name": "AB+" },
{ "Id": "8", "Name": "AB-" }];
$scope.BloodGroup_List = $scope.bloodgroup;
The above code is not working, where am I wrong?
You need to map the data source to the corresponding attributes in "fields" property.
<input type="text" id="bloodgroup" ej-dropdownlist e-datasource="BloodGroup_List" e-fields-text="Name" e-fields-value="Name" e-fields-id="Id" e-value="value">
In the script, inject 'ejAngular' and specify your values as shown below
angular.module('DropCtrl', ['ejangular'])
.controller('DropDownCtrl', function ($scope) {
$scope.bloodgroup = [{ "Id": "1", "Name": "O+" },
{ "Id": "2", "Name": "O-" },
{ "Id": "3", "Name": "A+" },
{ "Id": "4", "Name": "A-" },
{ "Id": "5", "Name": "B+" },
{ "Id": "6", "Name": "B-" },
{ "Id": "7", "Name": "AB+" },
{ "Id": "8", "Name": "AB-" }];
$scope.BloodGroup_List = $scope.bloodgroup;
$scope.value = "AB-";
});
This will work for you.
Try this..
<select ng-model="selectedItem">
<option ng-repeat="bloodgroupvalue in bloodgroup" value="{{bloodgroupvalue.Id}}">{{bloodgroupvalue.Name}}</option>
</select>
or
<select ng-model="selectedItem" ng-options="bloodgroupvalue.Name for bloodgroupvalue in bloodgroup">
</select>
You need to use e-fields-text="Name" to show text on in the dropdown then it will bind the value to that ng-model
HTML
<input id="test" type="text" ej-dropdownlist e-datasource="BloodGroup_List" e-fields-text="Name"
e-fields-value="Name" ng-model="employee.bloodgroup" e-change="selectedBloodGroup"/>
CODE
$scope.selectedBloodGroup = function(args) {
$scope.employee.bloodgroup = args.value;
$scope.$apply();
};
Working Plunkr