Iam trying to generate a multiple select-element with ng-options and want to preselect some items. I get the preselected items from a database as an array of objects. That is my HTML markup to build the selectable:
<select ng-model="currentAppointment.services" ng-options="value.id as value.name group by value.group for value in servicegroups"></select>
The currentAppointment.services variable hold the current selected objects. In servicegroups they are all selectable items, which i get from a REST service. The JSON for the servicegroups look like:
[
{
"id": 0,
"name": "Test1"
"price": 20.0
},
{
"id": 1,
"name": "Test2"
}
]
And the currentAppointment JSON:
{
"_id": "1001",
"title" : "test",
"services": [
{
"id": 0,
"name": "Test1"
"price": 20.0
}
]
}
The select-element is created correctly. But the service object with the id 0 is not preselected. I can image that the problem is, that angular compare an normal object against an angularjs Resource-object and so the objects are not equal. Instead the objects should be compared by the id. But how can i achieve that without coding a own directive?
If you want to pre-populate the dropdown list by assigning it with an object, then you need to remove value.id from the comprehension expression.
<select ng-model="currentAppointment.services" ng-options="value.name group by value.group for value in servicegroups"></select>
then do
$scope.currentAppointment.services = $scope.servicegroups[0];
Working Demo
Related
I am new in AngularJS and trying to show data in drop down list and facing few errors.
Here is my JSON data:
$scope.list=[
{
"$id": "1",
"Groups": [
"xyz",
"abc",
"ugh",
"ccd"
]
}
]
I am trying to show list of names of Groups mentioned in the json.
In controller :-
$scope.selectedval = $scope.list[0].Groups;
console.log($scope.selectedval);
In View I am doing this :-
ng-options="item.Groups for item in list"
By doing this I am getting all the datas in single option. How to separate datas on each list by ng-option ?
`ng-options="item for item in list[0].Groups"
In view try this
ng-options="val for val in selectedval"
$scope.list:[{"$id": "1",
"Groups": [
"xyz",
"abc",
"ugh",
"ccd"
]}]
$scope.list=[] // replace ":" by "="
so what I want to accomplish is populating a select element with a recurisve json, like so:
[{
"siteId": 1,
"name": "Test 1",
"siteTypeId": 2,
"childSites": [
{
"siteId": 3,
"name": "Test 3",
"siteTypeId": 2,
"childSites": [
{
"siteId": 2,
"name": "Test 2",
"siteTypeId": 2,
"childSites": []
}
]
},
{
"siteId": 4,
"name": "Test 4",
"siteTypeId": 2,
"childSites": []
}
]
}]
I found two ways I could approach this, either converting the structred object into a flat array, and then using ngOptions, or using the following code (which include ng-repeat with a custom template)
<script type="text/ng-template" id="sites-template">
<!--The object itself-->
<option>{{item.name}}</option>
<!--child items (if any), recursive call -->
<div ng-if="item.childSites && item.childSites.length > 0"
ng-repeat="item in item.childSites"
ng-include="sites-template">
</div>
</script>
<select class="selectpicker" data-live-search="true">
<div ng-repeat="site in sites" ng-include="sites-template"></div>
</select>
But when I run it with the json supplied above, it result with an empty select, can anyone explain why does it happen? I guess the correct solution would be to flattend the structured object, but I'm curious...
It seems that applying ng-repeat on an element inside a select tag, the child of the element would not recieve the value (when I removed the select tag it worked, as well of removing the wrapper and applying the ng-repeat straight on the option element, without the template), why is that?
Thanks.
I'm trying to use Angular Bootstrap Typeahead on an array of nested objects and I cannot figure out how to write out the typeahead.
My objects in my array are like so:
{
"category": "Locations",
"regions": [
{
"name": "Northeast",
"category": "region",
"states": [
{
"name": "New York",
"category": "state"
"cities": [
{
"name": "Syracuse",
"category": "city"
}
]
}
]
}
I only want to return the name values. So how would I go about writing this out?
I currently am writing <input ... typeahead=" filter.name for filter in filters| filter:$viewValue | limitTo:5">
Rather than using "in filters" just do something like "in transformFilters()"
$scope.transformFilters = function () {
// Loop over filters and create an array of
{name: name, category:cat}
return my new array
}
I have the following JSON file to read and I want to only display some particular keys I want from the JSON.
[
{
"recommendation_id": "cuhwbchuwbcuhw-ccnahcbhb-12",
"title": "hellow world",
"content": "Hello angular",
"name": "John",
"home": "USA"
},
{
"recommendation_id": "cuhwcacabchuwbw-ccnahcbhb-32",
"title": "ng-show",
"content": "ng-show is amazing",
"name": "Google",
"home": "USA"
},
......
{
"recommendation_id": "guwqiwu212wbcuhw-ccnahcbhb-12",
"title": "Awesome",
"content": "Hello Awesome",
"name": "Mike",
"home": "Canada"
}
]
Here is what I use to access the header for my table using Jade.
table(id="allRecommendations")
thead
tr
th(ng-repeat='(key, value) in recommendationsAll[1]') {{key}}
And the output is
recommendation_id title content name home
But I only want the first three items, which should display like the following:
recommendation_id title content
So, what do I miss on my filter? I am interested in getting the object properties not my property details. How do I filter to show particular JSON properties?
This will solve the problem.
table(id="allRecommendations")
thead
tr
th(ng-repeat="(key, value) in recommendationsAll[1]", ng-show="key == 'recommendation_id' || key == 'title' || key == 'content' " ) {{key}}
Plunker
I have some json that is being brought into a view via ng-repeat. I have a datepicker directive that sets a date variable in the model (I called the date todaysDate).
How do I show only the items that fit the date the user specifies? So let's say I have a bunch of json that looks like this, but has a variety of items with different dates.
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.4352424165, 37.23259223224644]
},
"properties": {
"id": "0",
"date": "2014-07-12",
"time": "22:00:00",
"artist": "heheheh",
"venue": "Place",
"price": "$20"
}
Let's say there's 500 items/events with different dates. I'll use the current date (aka 'today') as the default mode for items/events to show. I want the user to be able to specify which ones they want to see (should they want to see items happening in the future).
Plunker
You can add a filter to your ng-repeat:
<div ng-repeat="item in aBunchOfItems | filter: {properties: {date: '2014-07-12'}}">