Data Binding to a specific item of an array in Angular - javascript

Given a data structure that contains an array of JavaScript objects, how can I bind a certain entry from that array to an input field using Angular?
The data structure looks like this:
$scope.data = {
name: 'Foo Bar',
fields: [
{field: "F1", value: "1F"},
{field: "F2", value: "2F"},
{field: "F3", value: "3F"}
]
};
The fields array contains several instances of the given structure, with each entry having both a field attribute and a value attribute.
How can I bind an input control to the value field attribute of the array entry with the field F1?
<input ng-model="???"/>
I know that I could bind all fields using an ng-repeat, but that's not what I want. The above data is just an example from a much larger list of fields, where I only want to bind a pre-defined subset of fields to controls on the screen. The subset is not based on the attributes in the array entries, but is known at design time of the page.
So for the above example, I would try to bind F1 to one input on the page, and F2 to another one. F3 would not be bound to a control.
I've seen examples where a function was used in the ng-model, but it doesn't seem to work with Angular 1.1.0.
Is there another clever way to bind the input field to a specific array entry?
Here's a fiddle that has an example, but does not work since it's trying to use function in the ng-model attribute: http://jsfiddle.net/nwinkler/cbnAU/4/
Update
Based on the recommendation below, this is what it should look like: http://jsfiddle.net/nwinkler/cbnAU/7/

I personally would reorganize the array in a way that field property of an entry of the array become the identifier of the object. Mhhh that sentence may sound strange. What I mean is the following:
$scope.data = {
name: 'F1',
fields: {
F1: {
value: "1F"
},
F2: {
value: "2F"
}
}
};
If you want to bind a the value dynamically and it's an easy and quick way to achieve it.
Here is your fiddle modified so that it words. http://jsfiddle.net/RZFm6/
I hope that helps

You can use an array of objects, just not an array of strings.
HTML:
<div ng-repeat="field in data.fields">
<input ng-model="field.val"/>
</div>
JS:
$scope.data = {
name: 'F1',
fields: [
{ val: "v1" },
{ val: "v2" }
]
};
I've updated #Flek's fiddle here: http://jsfiddle.net/RZFm6/6/
Edit: Sorry just read your question properly, you can still use an array with:
<label>Bound to F1:</label>
<input ng-model="data.fields[0].value"/>
though maybe stop and think. Is there going to be variable number of fields ? or are you making a predetermined number of fields ? Use an array in the former and an object for the latter.

One way to do it is to simply add the necessary references to the scope, like this:
$scope.fieldF1 = fieldValue('F1');
$scope.fieldF2 = fieldValue('F2');
And then use those references:
<input ng-model="fieldF1.value"/>
<input ng-model="fieldF2.value"/>
Fiddle: http://jsfiddle.net/cbnAU/5/
Note: I'm assuming that $scope.data is static, but if it happens to be dynamic you can always watch for changes on it and recalculate the references...

Related

AngularJS Material - ng-checked on md-radio-button with ng-value as an object

I've looked around to see if anyone has answered this and it appears they have not. I want to use an object rather than a string or integer as the value of the radio button. Is this possible? Doesn't seem so because I'm having trouble with the tags md-radio-button recognizing an object rather than string or integer value as having been selected. I can see that it works after the page loads and I select something but I don't know how to check the radio button if the value already exists. You can see a very simple demonstration here: https://codepen.io/anon/pen/ZmjrLN
I've tried class="md-checked" to see if that works, it does not. I've tried ng-checked="selectedStatus.Name == status.Name", it doesn't work either. In fact ng-checked="true" also does not work.
I would think md-radio-button could work with an object!
--- EDIT FOR CLARIFICATION ---
From an answer below, referencing the code from codepen, if I use the same object in $scope.statuses to populate $scope.selectedStatus, it indeed selects the correct radio button on load. HOWEVER, in the real world $scope.selectedStatus is populated with the actual status from the server and $scope.statuses is also populated from that same call. They are the same but 2 different objects.
In a nutshell, I still want to check off the correct one, even though the objects aren't exactly the same, they should be treated like they are, because they are the same.
When comparing objects, ng-checked looks to see if checked is the same object or a reference and does not check the contend of the object. So to set the selected / checked radio button, make the value refer to the object. Here is an example of the Javascript updates for your codepen:
angular
.module('BlankApp')
.controller('AppController', function($scope) {
$scope.statuses = [
{id: 1, Name: 'Active'},
{id: 2, Name: 'Dormant'}
];
$scope.selectedStatus = $scope.statuses[0]; // reference the object directly
});
Or if the value is set outside of the scope and we have to compare, then we can compare manually and then set the selectedStatus by finding the correct object and assigning it.
angular
.module('BlankApp')
.controller('AppController', function($scope) {
$scope.statuses = [
{id: 1, Name: 'Active'},
{id: 2, Name: 'Dormant'}
];
$scope.selectedStatus = {id: 1, Name: 'Active'};
// Find the index of the selected that matches on id
let i = $scope.statuses.findIndex((val) => {
return val.id = $scope.selectedStatus.id;
});
$scope.selectedStatus = $scope.statuses[i]
});

Knockout select bindings overwriting my predefined observable object

Hi i have a application which data is passed from one page to another with predefined data objects/arrays assigned to it, my issue is i can see the observableArray having a value and then it turns the SelectedPeople observable to undefined.
I have eliminated down to the data bind markup as when i remove that my observable array does not set anything to undefined.
Here is how i am binding my observables/observableArray to the elements.
<select data-bind="options: ObservableArray.People, value: ObservableArray.SelectedPeople, optionsText: 'Name'"></select>
ObservableArray.People = Observable Array of objects - works fine and renders all the dropdown options
ObservableArray.SelectedPeople = Observable
Both have the 'Name' object defined to match the optionsText. It works perfectly when selecting data from scratch but when i try have predefined data in it the Observable.SelectedPeople object keeps getting sent as undefined when it tries to load.
Basically my Observable.SelectedPeople has a object on that which should predefined the value of that select and the object 100% matches one of the dropdown ObservableArray.People options. I need it not to set Observable.SelectedPeople to undefined and populate the select box.
Can anyone see why this is happening.
Thanks
...and the object 100% matches one of the dropdown ObservableArray.People options.
This line makes me suspicious of whether you're using an actual reference to the object, or just an object that is similar.
For example, this will not work:
var options = [{ id: 1 }, { id: 2 }, { id: 3}];
var selectedOption = ko.observable({ id: 1 });
Knockout does not perform some sort of deepEquals comparison; if it sees a non-primitive, it does a reference check. options[0] !== { id: 1 }, so this initial selection is not valid.
The code below will work, because you're using an actual object from the array you use in your select element:
var options = [{ id: 1 }, { id: 2 }, { id: 3}];
var selectedOption = ko.observable(options[0]);

Working with numbers in forms

I'm building an app with Aurelia and really liking the framework so far, but I've stumbled upon an issue where I'm trying to display a list of checkboxes whose values are numbers (ID:s in reality) and Aurelia seems to convert them to strings and thus comparison fails.
I basically have something like:
export class MyVm {
constructor () {
this.items = [
{name: 'Foo', id: 0},
{name: 'Bar', id: 1},
{name: 'Baz', id: 2}
];
this.selectedItems = [0, 2];
}
}
And in my view:
<ul>
<li repeat.for="item of items">
<input type="checkbox" value.bind="item.id" checked.bind="selectedItems">
${item.name}
</li>
</ul>
For this to work I actually have to do this.selectedItems = ["0", "2"] which just leads to a bunch of other comparison problems in my own code. I also don't want to send the selected item as a string to the server later on when saving the data.
I've tried using a simple value converter that converts toString toView and parseInt fromView, but I can't run this converter on the array of selectedItems:
export class IntValueConverter {
toView (val) {
return val.toString();
}
fromView (val) {
return parseInt(val);
}
}
How would you go about solving this?
You almost have it. There's one small problem with this part:
<input type="checkbox" value="${item.id}" checked.bind="selectedItems">
The <input> element's value attribute coerces everything it's assigned to a string. Not only that, a string interpolation binding (eg ${...}) also coerces everything to a string. You need to preserve the numeric item id. Replace value="${item.id}" with model.bind="item.id" and it will work. No need for a converter.
Coincidentally I just pushed a set of exhaustive docs on checkbox, radio and select binding to the Aurelia binding repo. They haven't been published to the official Aurelia docs app yet but they should be there on Tuesday.
Also- if you see anything weird with items whose id is zero- there's a fix going out on Tuesday for that as well.
Finally, I know this is not your question, but for others that land here looking for binding numbers in forms, here's a couple basic examples using a custom element and custom attribute:
https://gist.run/?id=d9d8dd9df7be2dd2f59077bad3bfb399

angular How to access dynamic multiple level data?

I have some data that I would like to loop through with ngRepeat and access data dynamically that's either on the top level or nested under a couple of levels.
However, it seems like I can get the top level properties to show up, but not when its nested.
JS:
var mapping = [
{property:'a'}, // works
{property:'b.c1'}, // doesn't work
{property:'b.c2'} // doesnt work
];
var arr = {
a: 'text',
b: {
c1: 'text2',
c2: 'text3'
}
};
HTML:
<div ng-repeat="mappingItem in mapping">
<p>{{arr[mappingItem.property]}}</p>
</div>
You can use angular's $parse service to evaluate the expression against arr:
$scope.map = function(property) {
return $parse(property)(arr);
};
<div ng-repeat="mappingItem in mapping">
<p>{{map(mappingItem.property)}}</p>
</div>
http://plnkr.co/edit/tPYcyT4HhqaepJZl0kd8?p=preview
I doubt your approach will work. Reason being the way you are trying to access the object is incorrect, i.e.: arr[mappingItem.property] will become arr['b.c1'] but your object doesn't have a key 'b.c1'. #DivyaMV's suggestion won't work either because property b in the mapping isn't an object hence it doesn't have a key c1. I'd suggest you either change the structure of your object OR you change how you are trying to access your object i.e. use ng-if to check for multiple levels, OR instead of looping, display the properties one by one
If you use {"property":"b","chiled":"c1"} type of structure then it works.
var mapping = [
{property:'a'},
{"property":"b","chiled":"c1"},// works
{property:'b.c2'}
];
<div ng-repeat="mappingItem in mapping">
<p>{{arr[mappingItem.property][mappingItem.chiled]}}</p> <!-- it works for second object-->
</div>

Angular Js: Can you set the default value for a select in conjunction with using 'track by' in ng-options

The best method to set the values for your options in your select list, seems to be to use 'track-by' (using a.id as a.name seems to be out-dated) in your ng-options directive. However, when I use 'track by', I am no longer able to set the default value using ng-model.
Take for example, this plunkr:
http://plnkr.co/edit/OZCal9ZkCQeqnQJY0WP9?p=preview
<select ng-model="class.team.leader" ng-options="student.name for student in curTeam.students track by student._id">
Currently, the plunkr correctly sets the default value. However, if you change it to use 'track by', like in the code above, it does not work.
track by doesn't work like that. It's used to compare a property on both objects rather than checking for object equality. ie obj1.trackBy === obj2.trackBy rather than obj1 === obj2.
For example, change your default model to:
$scope.class = {
team: {
leader: { _id: 2, name: 'Dave', status: 0 }
}
};
Without track by this wouldn't work as it's a different object to what's in the students array. But with track by angular will compare the _id properties to decide equality.

Categories

Resources