Ionic radio group not reflecting in model - angularjs

So I'm loading a bunch of questions from an api, every question has its own set of answers.
Im unable to populate the ng-model for radio box to record the selected answer
html
<div class="list">
<div class="item item-text-wrap" ng-repeat="question in vm.questions track by $index">
<h2>{{question.text}}</h2>
<ion-list if="question.type='mcq'">
<ion-radio ng-model="vm.answers[$index].answer_text" ng-value="'{{option.text}}'" ng-repeat="option in question.options" name="vm.answers[$index].answer_text">{{option.text}}</ion-radio>
</ion-list>
<input type="text" ng-if="question.type=='simple'" ng-model="vm.answers[$index].answer_text" placeholder="{{question.text}}"/>
</div>
</div>
relevalnt code from the controller
vm.answers = [];
api.need_assesment_questions.query(
//on success
function success(response) {
vm.questions = response;
//populate answers
for (var i = 0; i < vm.questions.length; i++) {
vm.answers.push({ question_text: vm.questions[i].text, answer_text: '' });
}
},
//on error
function error(response) {
alert(response.message);
}
);
The issue im facing is that answers array always rewrites the first answer text
fiddle : http://jsfiddle.net/pn2qL/76/

You need to initialise the question index on top and use it in your model, because there is another ng-repeat which override the previous $index
Try this
<div class="list">
<div class="item item-text-wrap"
ng-repeat="question in vm.questions track by $index"
ng-init="qIndex = $index">
<h2>{{question.text}}</h2>
<ion-list if="question.type='mcq'">
<ion-radio ng-model="vm.answers[qIndex].answer_text" ng-value="option.text" ng-repeat="option in question.options" name="{{vm.answers[qIndex].answer_text}}">{{option.text}}</ion-radio>
</ion-list>
<input type="text" ng-if="question.type=='simple'" ng-model="vm.answers[$index].answer_text" placeholder="{{question.text}}" />
</div>
</div>
Working fiddle

Related

get value from ng-repeat radio buttons

I have ng-repeat of radio buttons:
<div ng-repeat="c in currencies">
<ion-radio ng-model="checkradio" ng-value='c.code' >
{{c.name}} {{c.code}}
</ion-radio>
</div>
I intend to $watch the model and the value of the selected radio buttons:
$scope.$watch('checkradio', function () {
$scope.checkradio = "";
})
I don't get any value from my radio buttons. Obviously, I'm doing something wrong. I tried other approaches but they didn't work. Can anyone suggest how to get the value from the radio buttons?
For binding you should have an object not a variabile.
Then in your controller you should have:
$scope.radios = [{name:"first", code:"1"}, {name:"second", code:"2"}];
$scope.model = { checked: "1" };//check the first element
and in your HTML:
<ion-radio ng-repeat="radio in radios" ng-model="model.checked" ng-value="radio.code">{{radio.name}}</ion-radio>
or
<div ng-repeat="c in currencies">
<ion-radio ng-model="model.checked" ng-value='c.code' >
{{c.name}} {{c.code}}
</ion-radio>
</div>
You can find a codepen here
try this, this may help you, Here is working fiddle
<div ng-controller="MyCtrl">
<div ng-repeat="(key,val) in currencies">
<input type="radio" ng-model="$parent.checkradioasd" ng-value='val.code' >
{{val.name}}
</div> <br>
<div>Selected : {{checkradioasd}}</div>
controller
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.currencies = [{name:'Option 1',code:1},{name:'Option 2',code:2}];
$scope.checkradio = $scope.currencies[0].code;
}

Angular binding input value to list item to check if it is same

I have an array of 'word-pair' pairs which I am populating on my screen. Out of pairs, words will be populated in the left side list and on right side I am trying to have input field and when I enter corresponding pair value, checkmark should be shown.
My controller
.controller('DashCtrl', function($scope) {
$scope.message='Welcome to Ionic'
$scope.word_pair = [
{'word':'Nitish', 'pair':'Patkar'},
{'word':'Mihir', 'pair':'Janaj'},
{'word':'Jannes', 'pair':'Stubbi'},
{'word':'Martin', 'pair':'Wolle'}
]
})
My HTML:
<div class="row">
<!-- Left half of the screen to hold list of words -->
<div class="col col-50" align="center">
<ion-list>
<ion-item ng-repeat="item in word_pair">
{{item.word}}
</ion-item>
</ion-list>
</div>
<!-- Right half of the screen to hold list of pairs -->
<div class="col col-50"">
<ion-list>
<ion-item ng-repeat="item in word_pair">
{{item.pair}}
<input type="text">
<span><i class="ion-checkmark"></i></span>
</ion-item>
</ion-list>
</div>
</div>
How can I implement such an input field here? on I can perhaps use ng-if directive.
Try this-
$scope.word_pair = [
{'word':'Nitish', 'pair':'Patkar'},
{'word':'Mihir', 'pair':'Janaj'},
{'word':'Jannes', 'pair':'Stubbi'},
{'word':'Martin', 'pair':'Wolle'}
]
$scope.partnerCheckList = {};
for(var v in $scope.word_pair){
$scope.partnerCheckList[$scope.word_pair[v].word] = $scope.word_pair[v].pair;
}
$scope.showPartner = {};
$scope.partnerCheck = function(p,i_p){
if($scope.partnerCheckList[i_p] == p){
$scope.showPartner[p] = true;
}
}
<input ng-model="pair" type="text" ng-change="partnerCheck(pair,item.word)">
<span ng-show="showPartner[pair]"><i class="ion-checkmark"></i></span>
I think this should work.

Create Dynamic ng-models for firebase use | ionic | firebase | angularjs

I'm trying to make an app where users can tag other registered users in a post.
So the idea is if the user creates a new post. there is an ng-repeat for all the users available and for each of them a checkbox is created.
After the users are checked they have to be stored with the post in firebase.
Now i cant seem to figure out how to dynamically add properties to my firebase push
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">Titel</span>
<input type="text" placeholder="Titel" ng-model="formData.title">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Beschrijving</span>
<input type="text" placeholder="Kaarten voor... etc" ng-model="formData.description">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Prijs</span>
<input type="text" placeholder=".-" ng-model="formData.price">
</label>
</div>
<ion-list ng-repeat="user in users">
<ion-checkbox ng-click="la()" ng-model="users[user.Username][user.id][user.voltooid == false]" >
</ion-checkbox>
</ion-list>
<div class="button" ng-click="test()">test</div>
The push code
$scope.formData = {};
$scope.test = function (){
console.log($scope.users);
console.log($scope.formData);
posts.push({
title: $scope.formData.title,
description: $scope.formData.description,
price: $scope.formData.price,
users: {
//dynamic data
}
});
}
I'm gonna give you an example how to retrieve and post dynamically your data.
Here is a factory to sync your data with a controller/ng-model, but you can include it in a controller.
.factory("Items", function($firebaseArray) {
var itemsRef = new Firebase("https://YOUR-FIREBASE/");
return $firebaseArray(itemsRef);
})
In your controller you retrieve your data in a scope
$scope.items = Items;
Then use the function of angularfire call $save that will update your data into your firebase.
$scope.la = function(){
$scope.items.$save($scope.users[user.name][check]);
}
Finally you can do something like that to retrieve and update your data
<ion-checkbox ng-click="la()" ng-model="users[user.name][check]">

ngModel checkbox not shown as selected

I'm having some trouble getting a checkbox to display the correct state (checked/unchecked) of my model. I have the following in my controller:
app.controller('PhotosCtrl', ['$scope', function($scope) {
$scope.form = {};
$scope.editPhotos = {
token: $scope.token,
idArray: $scope.idArray
};
}]);
My form looks like this:
<form accept-charset="UTF-8" name="editPhotosForm" ng-submit="submit(editPhotos);" multipart="true" novalidate>
<input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" ng-model="editPhotos.token" ng-init="editPhotos.token='<%= form_authenticity_token %>'">
<div class="results-label"><b>{{total_count}}</b> <span ng-if="total_count == 1">Photo</span><span ng-if="total_count != 1">Photos</span> Found</div>
<div>
<div class="col">
<a ng-repeat="r in results" class="card result-link">
<div class="content result">
<div class="caption">
<input type="checkbox" ng-model="idArray[r.id]">
</div>
<div class="image-container" ng-style="{'background-image': 'url(' + r.image_url + ')'}">
</div>
</div>
</a>
</div>
</div>
</form>
On my repeater element, I call ng-init="idArray[r.id] = 'false'" to initialize a key r.id = 'false' for each item in r. I don't know if I need to do this or not. I've tried the code without it and it makes no difference. I've also tried using ng-value-true and ng-value-false but those don't seem to be working for me.
Most of the other posts I've seen on this issue deal with simple variables (e.g. $scope.someVar = true rather than more complex structures like a hash.
Here is the structure of idArray:
$scope.idArray = {1290: "false", 1291: "true", 1292: "true", 1293: "false", 1294: "false", 1414: "false"};
This is generated by the ng-init in my repeater, since the ids of the photos can't be known beforehand.
Here is what results looks like:
{
id: 1290,
company_id: null,
image_url: "http://s3.amazonaws.com/mybucket/photos/images/000/001/290/original/214.JPG?1432217895"
}
Doing the following
ng-init="idArray[r.id] = 'false'"
You're assigning the string value 'false' into your object.
Can't you deal with that inside your controller?
$scope.idArray = {};
for (var i = 0; i < $scope.results.length; ++i){
$scope.idArray[$scope.results[i].id] = (i%2 == 0);
}
And removing the ng-init="" (which is, according to Angular doc, a bad practice https://docs.angularjs.org/api/ng/directive/ngInit ).
Another thing was the anchor element that was wrapping the checkbox element. This lead to the click event that was not triggered on the checkbox, but only on the anchor.
<div class="col">
<div ng-repeat="r in results" class="card result-link">
<div class="content result">
<div class="caption">
<input type="checkbox" ng-model="idArray[r.id]">
</div>
<div class="image-container" ng-style="{'background-image': 'url(' + r.image_url + ')'}">
</div>
</div>
</div>
</div>
fiddle :
http://jsfiddle.net/patxy/wak7pwwp/

AngularJS (jsFiddle included): Scope variable not updated before sent to filter

Here's the jsFiddle: http://jsfiddle.net/VSph2/274/
I'm trying to make a filter with checkboxes.
When the user clicks the checkbox, it adds the id to an array called color_ids. I know that's working because I print the array in the console.
However, when I try to combine that with a filter, it doesn't work. I try to pass the $scope.color_ids array, but it is always passing an empty array and not passing the array with values in them.
app.controller('IndexCtrl', ['$scope', "Product", "Color", function($scope, Product, Color) {
...
// this method is triggered by a checkbox
$scope.toggleColorFilter = function(color_id) {
var index = $scope.color_ids.indexOf(color_id);
if (index > -1) {
$scope.color_ids.splice(index, 1);
} else {
$scope.color_ids.push(color_id);
}
console.log($scope.color_ids); //<-- prints the array properly with the new values.
};
}]);
and a filter that isn't working:
app.filter('productFilter', function(){
return function(input, color_ids) {
console.log(color_ids); //<-- prints an empty array all the time [ ]
return input;
}
})
This is my HTML
<h2>Products</h2>
<div class="filters col-two" ng-controller="IndexCtrl">
<h3>Color</h3>
<div ng-repeat="color in colors">
{{color.name}} <input type="checkbox" ng-model="color_ids" ng-change="toggleColorFilter(color.id)">
</div>
<h3>Shape</h3>
<h3>Material</h3>
</div>
<div class="products col-ten" ng-controller="IndexCtrl">
<div class="product" ng-repeat="product in products | productFilter:color_ids">
<h3>
{{ product.name }}
</h3>
<div class="product-thumbs">
<div class="image-wrapper" ng-repeat="product_color in product.products_colors">
<img src="{{ product_color.color.image.url }}" width="75" height="40">
</div>
</div>
</div>
</div>
I want the filter to eventually only show products with a color_id that exist in the color_ids array.
You have three divs with ng-controller="IndexCtrl" in your JSFiddle example. This is the problem. Each time the Angular compiler finds ng-controller in the HTML, a new scope is created.
<div class="filters col-two" ng-controller="IndexCtrl">
<h3>Color</h3>
<div ng-repeat="color in colors">{{color.name}}
<input type="checkbox" ng-model="color_ids" ng-change="toggleColorFilter(color.id)">
</div>
</div>
<div class="products col-ten" ng-controller="IndexCtrl">
<div class="product" ng-repeat="product in products | productFilter:color_ids">
{{ product.name }}
</div>
</div>
Simpliest way is to place this code in one controller and it will print 2 similiar arrays in your console:
<div ng-controller="IndexCtrl">
<div class="filters col-two">
<h3>Color</h3>
<div ng-repeat="color in colors">{{color.name}}
<input type="checkbox" ng-model="color_ids" ng-change="toggleColorFilter(color.id)">
</div>
</div>
<div class="products col-ten">
<div class="product" ng-repeat="product in products | productFilter:color_ids">
{{ product.name }}
</div>
</div>
</div>
JSFiddle
The filter is applied before the color_ids is updated, you should apply the filter in the controller inside the toggle function:
$filter('productFilter')($scope.products, $scope.color_ids);
Here is the working findle (at least I think): http://jsfiddle.net/VSph2/276/
Don't forget to inject the $filter in your controller.

Resources