I'm trying to solve bind problem in my application. Similar questions are exists on Stackoverflow, but they do not fully describes my scenario.
I have an radio group created using repeater. Each style value is the Object so I'm using ng-value directive to bind them correct
<label ng-repeat="style in styles">
<input type="radio" ng-model="formData.style" ng-value="style">
{{style.name}}
</label>
And my controller logic is very simple:
var first = {
name: "First Name",
value: "First Value"
};
var second = {
name: "Second Name",
value: "Second Value"
};
var third = {
name: "Third Name",
value: "Third Value"
};
$scope.styles = [first, second, third];
$scope.formData = {};
//this code works and my screen shows 'second' as selected option
$scope.formData.style = second;
//this code do not works and my screen do not show 'Second name' as selected
//$scope.formData.style = {
// name: "Second Name",
// value: "Second Value"
// };
This code works as expected. I'm setting my selection and form shows selected option.
But in my particular example I don't have reference to my second value and I need to take this data from third control, so my updated code will looks like:
$scope.formData.style = {
name: "Second Name",
value: "Second Value"
};
And this behavior do not works - I do not see radio selection on my screen.
Here is my fiddle https://jsfiddle.net/vadimb/L7uw3oos/3/
$scope.formData.styleto an anonymous object which just happens to have the same properties with the same values, but your radio buttons aren't bound to the properties, they are bound to the specific object instances.$scope.formData.stylewhich in the first example is set tosecondwhich is an item within your$scope.stylesarray (which you are iterating over to display your radio buttons). You are not binding it to the correct object that is in the$scope.stylesarray, the items within$scope.stylesare their own objects, when you are writing$scope.formData.style = { ... }you are creating a brand new object