Quiz app - comparing values from inputs and JSON after button click - json

I wanted to make simple quiz app.
In my JSON file I have question with answers and points for an answer.
I want to list questions, gives text inputs for user to fulfill and a button to check answers. After clicking button I will show result and mark with green or red questions which were answered correct or incorrect.
I am able to list questions but I do not know how after clicking button I could go through all inputs and do all operations: (check them with corresponding answers from JSON file, add points, mark fields).
My JSON file is:
[
{
"id": 1,
"question": "How much is it: 2+2",
"answer": ["4", "four"],
"points": 2
},
{
"id": 2,
"question": "How much is it: 2*2",
"answer": ["4", "four"],
"points": 3
},
{
"id": 3,
"question": "How much is it: 2+2*2",
"answer": ["6", "six"],
"points": 4
}]
HTML:
<div class="col-sm-4">
<h3>What are Your answers? </h3>
<form>
<div class="form-group" ng-repeat="item in answers">
<label>Question (Id: {{item.id}}): {{item.question}}</label><br>
<input type="text" name="{{item.id}}" id="{{item.id}}">
</div>
<button type="submit" class="btn btn-default" ng-click="calculatePoints()">Check</button>
</form>
</div>
<div class="col-sm-4">
<h3>Number of points which You have:</h3>
<span>{{e}}</span>
</div>
Controller:
$scope.calculatePoints = function() {
};

first you need to give ng-model to input
secondly you can pass answer element and answer to the calculatepionts function
<input type="text" ng-model="item.currentAnswer"
name="{{item.id}}" id="{{item.id}}">
sending the question object and answer to the calculate function
<button type="submit" class="btn btn-default"
ng-click="calculatePoints(item,item.currentAnswer)">Check</button>
Now you can compare the answer with the orignal one in calculatePoints()

Related

Printing HTML in ng-repeat [duplicate]

This question already has answers here:
With ng-bind-html-unsafe removed, how do I inject HTML?
(10 answers)
Closed 4 years ago.
I have an ngController which contains, amongst other things, an array of questions and answers:
$scope.faqs = [
{
question: "QUESTION 1",
answer: "ANSWER 1"
},
{
question: "What is the dress code?",
answer: "See here."
},
{
question: "QUESTION 3",
answer: "ANSWER 3"
}
];
In my HTML I then cycle through these to display them:
<div>
<div ng-controller="FAQController" class="FAQ_container">
<div ng-repeat="faq in faqs">
<button class="collapsible">{{ faq.question }}</button>
<div class="content">
<p>{{ faq.answer }}</p>
</div>
</div>
</div>
<div>
However, for the middle question in my array, it prints the whole item as a string. I can understand why this is the case, however I would like for it to have the link clickable.
I have tried the suggestion from How to parse HTML in ng-repeat in angular.js by changing my
<p>{{ faq.answer }}</p>
to
<p ng-bind-html-unsafe="faq.answer"></p>
but that has just served to stop anything printing. Can anyone help with an alternative suggestion or fix please?
Please note: I am just starting out with angularjs and web development so please try to keep your answers simple and bear with me if I have to ask for clarification.
You could use ngSanitize and make your answers to be trusted has html like this:
angular.forEach($scope.faqs, function(faq) {
faq.answer = $sce.trustAsHtml(faq.answer);
});
For this you will need to use the $sce service.
And then bind them like this: <p ng-bind-html="faq.answer"></p>
See below working full sample:
angular.module('app', ['ngSanitize']).controller('FAQController', function($scope, $sce) {
$scope.faqs = [{
question: "QUESTION 1",
answer: "ANSWER 1"
},
{
question: "What is the dress code?",
answer: "See here."
},
{
question: "QUESTION 3",
answer: "ANSWER 3"
}
];
angular.forEach($scope.faqs, function(faq) {
faq.answer = $sce.trustAsHtml(faq.answer);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.14/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.14/angular-sanitize.js"></script>
<div ng-app="app">
<div ng-controller="FAQController" class="FAQ_container">
<div ng-repeat="faq in faqs">
<button class="collapsible">{{ faq.question }}</button>
<div class="content">
<p ng-bind-html="faq.answer"></p>
</div>
</div>
</div>
<div>

Laravel complains for missing non-nullable value, but it is there

I have the following code in Laravel 5.4.* to store an array of JSON objects.
Sample::where('contest_id', $request->get('contest_id'))
->where('type', '0')
->delete();
if ( $request->get('extra3') ) {
$samples = $request->get('samples');
//return $samples;
Sample::insert($samples);
}
I had an error for a missing value that is not nullable, so I started the debugging.
This is the return of the $samples, just before the encoding:
[
{
"id": 16,
"contest_id": "35",
"product_id": "2",
"supplier_id": "2",
"quantity": "5",
"type": "0",
"created_at": null,
"updated_at": null
},
{
"contest_id": 35,
"type": "0",
"product_id": "4",
"supplier_id": "3",
"quantity": 3
}
]
However, if I commented out the return and let the eloquent runs, the insert is messed up.
This is the full error
{"error":{"message":"SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: samples.supplier_id (SQL: insert into \"samples\" (\"contest_id\", \"created_at\", \"id\", \"product_id\", \"quantity\", \"supplier_id\", \"type\", \"updated_at\") select 1 as \"contest_id\", as \"created_at\", 5 as \"id\", 2 as \"product_id\", 5 as \"quantity\", 2 as \"supplier_id\", 0 as \"type\", as \"updated_at\" union all select 1 as \"contest_id\", 1 as \"created_at\", 1 as \"id\", 1 as \"product_id\", 0 as \"quantity\", ? as \"supplier_id\", ? as \"type\", ? as \"updated_at\")","code":"23000","status_code":500}}
Edit: A few more details how I reproduce the error. I don't have it on every insert request, but only after a certain process.
I have a dynamic form with Angular where user can create as many models of Sample as she wants. The first time the user send the request with the models, Laravel saves those without an error.
When I refresh the page and the form is created automatically with the data that I had in the DB, if I remove one of them and create a new one, but keep at least one of the previous data, then I have the error.
Just before the above code, I have a line that delete data from the same table, so everything that was there, isn't during the insert command.
HTML of the dynamic form:
<div class="row col-md-12" *ngFor="let input of initSamples; let i=index">
<div class="col-md-4">
<label *ngIf="i==0" for="product{{i}}" class="col-md-12">Product</label>
<div class="input-group">
<select name="product{{i}}" [(ngModel)]="input.product_id" id="product{{i}}" class="form-control">
<option *ngFor="let product of products" value="{{product.id}}">{{product.name}}</option>
</select>
</div>
</div>
<div class="col-md-4">
<label *ngIf="i==0" for="supplier{{i}}" class="col-md-12">Supplier</label>
<div class="input-group">
<select name="supplier{{i}}" [(ngModel)]="input.supplier_id" id="supplier{{i}}" class="form-control ">
<option *ngFor="let supplier of suppliers" value="{{supplier.id}}">{{supplier.name}}</option>
</select>
</div>
</div>
<div class="col-md-2">
<label *ngIf="i==0" for="quantity{{i}}" class="col-md-12">Quantity</label>
<input type="number" min=1 step=1 [(ngModel)]="input.quantity" id="quantity{{i}}" name="quantity{{i}}" class="form-control">
</div>
</div>

I'am receiving a json array from server, how to map this array with angular 2 formarray

my recieving json array looks something like this
questions = [
{
q_name="question 1",
op_1="op 1",
op_2="op 2",
answer=1
},
{
q_name="question 2",
op_1="op 1",
op_2="op 2",
answer=2
}
];
i want to display this data in a form with radio buttons to choose options
how to achieve this, im new to angular 2
You have to update your Question collection object in component as below.
//Define private variable
questions: any;
//List of questions
this.questions = [
{
"q_name":"question 1",
"op_1":"op 1",
"op_2":"op 2",
"answer":"op 1"
},
{
"q_name":"question 2",
"op_1":"op 3",
"op_2":"op 4",
"answer":"op 4"
}
];
Now, In HTML render radio button as below:
<div *ngFor="let question of questions;let i=index">
<div>
Question : {{question.q_name}}
</div>
<div>
<input type="radio" [(ngModel)]="question.answer" name="{{i}}" value="question.op_1">{{question.op_1}}<br />
<input type="radio" [(ngModel)]="question.answer" name="{{i}}" value="question.op_2">{{question.op_2}}<br />
</div>

Angular ng-show won't eval correctly

So if my JSON array object returns this:
"Tools": [
{
"name": "Submit a Claim",
"position": 1,
"isOn": true,
"alert": null
},
{
"name": "My Recurring Claims",
"position": 2,
"isOn": true,
"alert": null
},
{
"name": "Online Enrollment",
"position": 3,
"isOn": false,
"alert": "Online enrollment is available for the upcoming plan year. Click here to enroll!"
},
And my ng-show html has this:
<div class="toolTile col-md-3" ng-show="Tools.name = 'Online Enrollment' && Tools.isOn==true ">
<a href="#/claimEnter">
<img class="toolIcon" src="ppt/assets/toolIcons/oe.svg">
<p>Online Enrollment</p>
</a>
</div>
<div class="toolTile col-md-3" ng-show="Tools.name = 'Submit a Claim' && Tools.isOn==true ">
<a ng-click="transId()" ng-href="{{ ppt.Globals.hasDebitCard ? '#/claimEnter' : '#/claimSub' }}" >
<img src="ppt/assets/toolIcons/submitclaim.svg" >
<p>Submit a Claim</p>
</a>
</div>
Why does it keep evaluationing to false? I have tried quite a few variations and the "Online Enrollments" should hide while the "Submit a Claim" should show.
Any ideas of what I may be doing wrong here?
Thanks much.
Hi Man You could not Post Code with Out Head and Tail. There were Some
Syntax Errors Which caused the trouble .
You Need to Provide a Plunker with Buggy Code at least for some help :-) .
When you got an array in object you need to iterate over it.
I have provided you a running Plunker :-
<div ng-repeat="tool in data.Tools">
<div ng-show="tool.name == 'Online Enrollment' && tool.isOn==false ">
Online Enrollment
</div>
<div ng-show="tool.name == 'Submit a Claim' && tool.isOn == true ">
Submit a Claim
</div>
</div>
Plunker :-
http://plnkr.co/edit/B01pKWLsUtA2JlTAPOd5?p=preview
Good Luck !! Let me Know Your Queries if any !!

Looping through json passed through to assemble partial as variable

I'm having trouble trying to loop through my JSON data with an a assemble site setup in the following structure:
-src
--content
---index.hbs
--data
---steps-data.json
--partial
---steps.hbs
The index.hbs in the content calls the partial passing through the object like so:
{{> steps steps-data}}
My steps-data.json file looks like so:
{
"steps":[{
"index": 1,
"title": "Find a product",
"description": "Go to a product page and click on the +PriceTrack short cut to add to your list."
},{
"index": 2,
"title": "Track its price",
"description": "Go to a product page and click on the +PriceTrack short cut to add to your list."
},{
"index": 3,
"title": "Purchase",
"description": "Go to a product page and click on the +PriceTrack short cut to add to your list."
}]
}
In my steps.hbs i've tried looping through the JSON data but its not.
<div class="g-col g-span4">
{{#each steps-data.steps}}
<div class="working-step">
<span class="number"></span><h3>{{title}}</h3>
</div>
<p>{{description}}</p>
{{/each}}
</div>
The problem I have is that its not looping through and not sure what I'm doing wrong.
Since you passed steps-data into the partial as it's context, you can access steps directly or with this.steps:
<div class="g-col g-span4">
{{#each this.steps}}
<div class="working-step">
<span class="number"></span><h3>{{title}}</h3>
</div>
<p>{{description}}</p>
{{/each}}
</div>