angularjs Array push Not working in switch statement - angularjs

Angular js Push array not working in switch case it update last added element only
vm.saveModalData = function(title) {
if (!$scope.parentCtrl.docsList[vm.newDocObj.document_category]){
$scope.parentCtrl.docsList[vm.newDocObj.document_category] = [];
}
if(!title){
title = vm.docFileName;
$scope.parentCtrl.docsList[vm.newDocObj.document_category].push(vm.docFileName);
} else {
$scope.parentCtrl.docsList[vm.newDocObj.document_category].push(title);
}
var doc_obj = {};
doc_obj.title = title;
doc_obj.type = appConstants.DOCUMENT_CATEGORY[selectedCategory];
doc_obj.data = vm.newDocObj.document;
switch(selectedCategory) {
case 'PAN':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.pan_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Aadhaar':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.aadhar_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Driving License':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.driving_license_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Passport':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.passport_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Voter Id':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.voter_id_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Company IT Returns':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
case 'Company PAN':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
case 'ITR/VAT Returns/ST Returns':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
case 'Other':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
default:
break;
}
vm.closeModal();
}
How to fix this issue?
I try to add first document it added into array then i added one more document but it remove existing and add as a new.

Declare your array in parent controller and use $emit on $on for passing data between child to parent controller. Try like below.
function docLpController($scope, $q, appConstants, $uibModal, utils) {
var vm = this;
$scope.parentCtrl = $scope.$parent.finwizzCtrl;
$scope.parentCtrl.completeDetails.documents = []; // declared
$scope.$on('sendData', function (event, data) {
$scope.parentCtrl.completeDetails.documents.push(data);
});
vm.saveModalData = function(title){
$scope.$emit('sendData', doc_obj);
}

Try this,
You are calling docCtrl.openAddDocModal(doc) function, while adding each and every time of Document add. Inside that function only you have initialised your array.
$scope.parentCtrl.completeDetails.documents = [];
so that each and every time it's getting reinitialised with empty.
Add this code line out side of the $uibModal.open(), recommended add below $scope.arr = [];
hope it will work...

Related

How to use fields and variables to save data permanently in Google Actions with Dialogflow?

I'm trying to build up a timetable-Google-Action. The Action is supposed to tell all the subjects of a certain day when calling.
The Problem is, how do I save the data permanently on my device to access the data at any time?
I tried to declare fields in the fulfillment NODE.JS-File, but that did not worked at all. (I realised that it was a really stupid idea, as the code does not work as a 'programm', so there is no compiled stuff running in the background).
The next thing I tried was the code snippet on the Google-Developer website (https://developers.google.com/actions/assistant/save-data#df_nodejs) -> Snippet 1
I don't have any further knowledge of Java-Script or Node.JS or Google Actions in Dialogflow, but my teacher told us to develop an application without giving any information about that topic.
You can see my Fulfillment Code in Snippet 2
Snippet 1
function simpleResponse(conv) {
conv.user.storage.count = 1;
conv.ask('When is your birthday?');
}
Snippet 2
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
/* FUNCTIONS */
function getSubjects(agent) {
const date = agent.parameters.date;
const dateIso = new Date(date);
const weekDayInt = dateIso.getDay();
var dayString = '';
switch (weekDayInt){
case 0: dayString='Sonntag';
var output = agent.user.storage.sun.join(' ');
break;
case 1: dayString='Montag';
var output = agent.user.storage.mon.join(' ');
break;
case 2: dayString='Dienstag';
var output = agent.user.storage.tue.join(' ');
break;
case 3: dayString='Mittwoch';
var output = agent.user.storage.wed.join(' ');
break;
case 4: dayString='Donnerstag';
var output = agent.user.storage.thu.join(' ');
break;
case 5: dayString='Freitag';
var output = agent.user.storage.fri.join(' ');
break;
case 6: dayString='Samstag';
var output = agent.user.storage.sat.join(' ');
break;
}
agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
agent.add(new Card({
title:`Dein Stundenplan für `+dayString+`:`,
//imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
text: 'Du hast heute'+output+' .',
buttonText: 'This is a button',
buttonUrl: 'https://assistant.google.com/'
})
);
agent.add(new Suggestion(`Stundenplan für heute`));
agent.add(new Suggestion(`Stundenplan für morgen`));
}
function setSubject(agent){
const subject = agent.parameters.subject;
const date = agent.context.get('date');
const dateIso = new Date(date);
const weekDayInt = dateIso.getDay();
var dayString = '';
switch (weekDayInt){
case 0: dayString='Sonntag';
agent.user.storage.sun.push(subject);
break;
case 1: dayString='Montag';
agent.user.storage.mon.push(subject);
break;
case 2: dayString='Dienstag';
agent.user.storage.tue.push(subject);
break;
case 3: dayString='Mittwoch';
agent.user.storage.wed.push(subject);
break;
case 4: dayString='Donnerstag';
agent.user.storage.thu.push(subject);
break;
case 5: dayString='Freitag';
agent.user.storage.fri.push(subject);
break;
case 6: dayString='Samstag';
agent.user.storage.sat.push(subject);
break;
}
}
let intentMap = new Map();
intentMap.set('Stundenplan', getSubjects);
intentMap.set('WhichSubject', setSubject);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
/////
UPDATE
/////
I now tried your code, but it did not functioned at all. Google interpretes the day wrong whithout any changes of the code (I'm a bit confused) and the string (whith the subjects) can't be saved neither.
It would be great if you'll take a loook at my new fulfillment code again and tell me what is wrong.
Snippet 3
'use strict';
const functions = require('firebase-functions');
const {dialogflow, BasicCard, Suggestions} = require('actions-on-google');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
const app = dialogflow();
app.intent('GetTimeTable', (conv, {date}) => {
const dateIso = new Date(date);
const weekDayInt = dateIso.getDay();
let dayString = '';
let output = '';
switch (weekDayInt) {
case 0:
dayString = 'Sonntag';
output = conv.user.storage.sun;
break;
case 1:
dayString='Montag';
output = conv.user.storage.mon;
break;
case 2:
dayString='Dienstag';
output = conv.user.storage.tue;
break;
case 3:
dayString='Mittwoch';
output = conv.user.storage.wed;
break;
case 4:
dayString='Donnerstag';
output = conv.user.storage.thu;
break;
case 5:
dayString='Freitag';
output = conv.user.storage.fri;
break;
case 6:
dayString='Samstag';
output = conv.user.storage.sat;
break;
}
conv.ask(`Du hast am ${dayString} ${output}`);
conv.ask(new BasicCard({
title: `Dein Studenplan für ${dayString}:`,
text: `Du hast ${dayString} ${output}.`,
}));
});
app.intent('WhichSubject', (conv, {date, subject}) => {
const dateIso = new Date(date);
const weekDayInt = dateIso.getDay();
let dayString = '';
//conv.user.storage.sun = 'Deutsch';
switch (weekDayInt){
case 0:
dayString='Sonntag';
conv.user.storage.sun = ''+subject;
break;
case 1:
dayString='Montag';
conv.user.storage.mon = ''+subject;
break;
case 2:
dayString='Dienstag';
conv.user.storage.tue = subject;
break;
case 3:
dayString='Mittwoch';
conv.user.storage.wed = subject;
break;
case 4:
dayString='Donnerstag';
conv.user.storage.thu = subject;
break;
case 5:
dayString='Freitag';
conv.user.storage.fri = subject;
break;
case 6:
dayString='Samstag';
conv.user.storage.sat = subject;
break;
}
conv.ask(`Am ${dayString} das Fach ${subject}.`);
conv.ask(new BasicCard({
title: `Neu gespeichert:`,
text: `Am ${dayString} das Fach ${subject}.`,
}));
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Your main issue is that in the first snippet it assumes the actions-on-google library instead of the dialogflow-fulfillment library.
The userStorage feature is only available for Actions on Google, so you would need to transition all of your code to use actions-on-google instead of dialogflow-fulfillment.
Here's a quick example of how you could adjust your code to use the library. This is not a complete set of changes, but should help you get started.
const functions = require('firebase-functions');
const {dialogflow, BasicCard, Suggestions} = require('actions-on-google');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
const app = dialogflow();
app.intent('Stundenplan', (conv, {date}) => {
const dateIso = new Date(date);
const weekDayInt = dateIso.getDay();
let dayString = '';
let output = '';
switch (weekDayInt) {
case 0:
dayStrin = 'Sonntag';
output = conv.user.storage.sun.join(' ');
break;
// ...
}
conv.ask('This message is from the Dialogflow Cloud Function for Firebase editor');
conv.ask(new BasicCard({
title: `Dein Studenplan für ${dayString}:`,
text: `Du hast heute ${output}.`,
buttonText: 'This is a button',
buttonUrl: 'https://assistant.google.com/'
}));
conv.ask(new Suggestions('Stundenplan für heute', 'Stundenplan für morgen'))
})
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

How to instantiate an array in TypeScript which should contain strings?

I am new to TypeScript and working on a server monitoring webApp. I have a method which should save the status of pings and endpoints into an array. Then it should determine the status of the servers depending on the entries in that array. The method should be working correctly I assume, but I think I am not initialising the array in a proper way.
setServersStatus() {
let allStatus: String[] = new Array(); // Here I get a Warning "Instantiation can be simplified"
for (let server of this.servers) {
if (server.restendpoints != null) {
for (let rest of server.restendpoints) {
switch (rest.status) {
case "OK":
allStatus.push("OK");
break;
case "WARNING":
allStatus.push("WARNING");
break;
case "ERROR":
allStatus.push("ERROR");
break;
default:
console.log('status empty');
}
}
}
if (server.ping != null) {
switch (server.ping.status) {
case "OK":
allStatus.push("OK");
break;
case "WARNING":
allStatus.push("WARNING");
break;
case "ERROR":
allStatus.push("ERROR");
break;
default:
console.log('status empty');
}
}
if (allStatus.indexOf('ERROR')) {
server.status = 'ERROR';
}
else if (allStatus.indexOf('WARNING')) {
server.status = 'WARNING';
}
else if (allStatus.indexOf('OK')) {
server.status = 'OK';
}
allStatus.length = 0;
}
}
I also tried to initialize in the following way, but it didn't work:
let allStatus = []; // Here it says "Variable allStatus implicitly has an any[] type"
So how do I initialize an array properly in TypeScript?
You can declare a typed array like this
let allStatus: string[] = [];
or like this
let allStatus: Array<string> = [];

Show and close dynamically created dialog in angularjs

I have scenarios which are created dynamically by clicking Add Scenario button. Initially 1 scenario will be there and then user can add 4 scenarios. Maximum 5 scenarios can be added. Each scenario has a calculator icon and by clicking it, it will show a calculator popup.
If user clicks calculator icon on a particular scenario, it should show only particular scenario's calculator popup; and if user clicks close, it should close the popup.
I'm passing scenarios.Id in showCalculatorPopup(scenarios.Id) and CloseCalculatorPopup(scenarios.Id) and with scenarios.Id i'm trying to open and close particular scenario's popup.
My issue is if I click a calculator icon in scenario 1, popup is being opened in all scenarios. I tried using eval(), window[] to no avail.
Updated the code.
Instead of Switch Case i'm using
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = true;
}
Added break;. Even after adding break it's not working properly. When i open and close calculator for 1st time in scenario 1 then its working fine.
But when i add scenario 2 and click calculator icon, its shows the popup and when i click close in scenario 1 popup it close the scenario 2 popup.
Can't use index because button is inside the Licenceplates and its inside the Sections and its inside the Scenarios.
<div data-ng-app="ang" data-ng-controller="InputController as input">
<div data-ng-repeat="scenarios in input.model.Scenarios"> <!-- Scenario Id = 1, 2, 3, 4, 5 -->
<div data-ng-repeat="sections in scenarios.Sections">
<div data-ng-repeat="licensePlates in sections.LicensePlates">
<button data-ng-click="input.showCalculatorPopup(scenarios.Id)">Calculate</button>
<div data-ng-show="input.calculatorPopup{{scenarios.Id}}">
Dialog Box
<button data-ng-click="input.CloseCalculatorPopup(scenarios.Id)"></button>
</div>
</div>
</div>
</div>
</div>
Latest Angular Controller
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = true;
}
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = false;
}
Previous Angular Controller
vm.showCalculatorPopup = function (ScenarioId) {
vm.eval('calculatorPopup' + ScenarioId) = true;
// or
vm.window['calculatorPopup' + ScenarioId] = true;
vm.calculatorPopup1 = true; // working
// or
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
}
vm.CloseCalculatorPopup = function (ScenarioId) {
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = false;
break;
//case 3 ,4
case 5:
vm.calculatorPopup2 = false;
break;
}
}
Your switch cases are never breaking, so you are executing every later case.
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
You have the same problem with the other swtich. More info on this.
you could pass $index instead of scenarios.Id. finally you need to add a break statement in your switch statements
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
}
vm.CloseCalculatorPopup = function (ScenarioId) {
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = false;
break;
case 2:
vm.calculatorPopup2 = false;
break;
}
}
break stops the evaluation of your switch statement.

In angularJS, insert information into function property

This is the function I made:
var _loadNieuws =
function (url) {
switch (url) {
case 'example1':
_nieuws = EXAMPLE1_NIEUWS;
break;
case 'example2':
_nieuws = EXAMPLE2_NIEUWS;
break;
}
}
Now I'm trying to give the url a value using my controller, this is how far I came: NieuwsService.loadNieuws.url = 'example1';
But then I get this error:
"Cannot set property 'url' of undefined".
This is my whole factory:
App.factory('NieuwsService', ['EXAMPLE1_NIEUWS', 'EXAMPLE2_NIEUWS', function (EXAMPLE1_NIEUWS, EXAMPLE2_NIEUWS) {
var nieuwsService = {};
var _nieuws = [];
var _loadNieuws =
function (url) {
switch (url) {
case 'example1':
_nieuws = EXAMPLE1_NIEUWS;
break;
case 'example2':
_nieuws = EXAMPLE2_NIEUWS;
break;
}
}
nieuwsService.loadNiews = _loadNieuws;
nieuwsService.nieuws = _nieuws;
return nieuwsService;
}]);
So my question is how do I give the property url in the function a value using my controller ?
Where is the question?
If you want to pass variable into the function put it as a parameter in function:
<div ng-click="_loadNieuws(loadNieuws.url)">Click here</div>
and your js-code will work fine
I think you need something like this in your controller :
$scope.url = NieuwsService.loadNieuws('example1');
This is the solution that worked fine for me:
var url = 'example1';
$scope.url = angular.copy(NieuwsService.loadNieuws(url));

flash as3 - declaring a null property within an object, or running a function within an object declaration

I have an object which is assigned a number of properties:
var project_array:Array = [];
var slideObject:Object = {
project_title : myXML.projects.project[i].title.toUpperCase(),
project_desc : myXML.projects.project[i].description.toUpperCase(),
project_name : myXML.projects.project[i].name.toUpperCase(),
project_agency : myXML.projects.project[i].agency.toUpperCase(),
project_img : myXML.projects.project[i].#img,
project_types : myXML.projects.project[i].#type.split(", ")
}
project_array.push(slideObject);
What I want to be able to do is, based on the values within slideObject.project_types, create another array within slideObject that keeps track of project_clips - like this:
for ( var i in project_types_array) {
/*(var typeClass:Class = getDefinitionByName('type_' + project_types_array[i]);
(var typeClip:typeClass = new typeClass();
project_clips_array.push(typeClip);
trace (project_types_array[i]);*/
switch (project_types_array[i]){
case "p":
var clip_p = new type_p();
project_clips_array.push(clip_p);
break;
case "exp":
var clip_exp = new type_exp();
project_clips_array.push(clip_exp);
break;
case "f":
var clip_f = new type_f();
project_clips_array.push(clip_f);
break;
case "oi":
var clip_oi = new type_oi();
project_clips_array.push(clip_oi);
break;
case "tv":
var clip_tv = new type_tv();
project_clips_array.push(clip_tv);
break;
}
}
but I'm not quite sure where to place this. If I place it outside of the object constructor, I get "term is undefined", I guess because it doesn't know what project_clips_array is - but if I declare project_clips_array in the constructor, it appears to need to be defined, i.e. I can't create a blank property. But I can't place it in the constructor either, because it doesn't seem to allow me to run a function within an object constructor. What is the proper syntax or arrangement of code for executing this function to get the array within the object?
I think something like this should work:
var project_array:Array = [];
var slideObject:Object = {
project_title : myXML.projects.project[i].title.toUpperCase(),
project_desc : myXML.projects.project[i].description.toUpperCase(),
project_name : myXML.projects.project[i].name.toUpperCase(),
project_agency : myXML.projects.project[i].agency.toUpperCase(),
project_img : myXML.projects.project[i].#img,
project_types : myXML.projects.project[i].#type.split(", ")
}
slideObject.project_type_array = sortTypes(slideObject.project_types);
project_array.push(slideObject);
function sortTypes(orig_array:Array):Array
{
var type_array:Array = [];
for ( var i in orig_array)
{
switch (orig_array[i])
{
case "p":
var clip_p = new type_p();
type_array.push(clip_p);
break;
case "exp":
var clip_exp = new type_exp();
type_array.push(clip_exp);
break;
case "f":
var clip_f = new type_f();
type_array.push(clip_f);
break;
case "oi":
var clip_oi = new type_oi();
new_array.push(clip_oi);
break;
case "tv":
var clip_tv = new type_tv();
type_array.push(clip_tv);
break;
}
}
return type_array;
}
I set it up like this:
for (var i=0;i<tp;i++){
var slideObject:Object = {
project_title : myXML.projects.project[i].title.toUpperCase(),
project_desc : myXML.projects.project[i].description.toUpperCase(),
project_name : myXML.projects.project[i].name.toUpperCase(),
project_agency : myXML.projects.project[i].agency.toUpperCase(),
project_img : myXML.projects.project[i].#img,
project_types : myXML.projects.project[i].#type.split(", "),
project_type_clips : [],
}
for (var j in slideObject.project_types){
//trace ("object" + i + " | type " + j)
switch (slideObject.project_types[j]){
case "p":
var clip_p = new type_p();
slideObject.project_type_clips.push(clip_p);
break;
case "exp":
var clip_exp = new type_exp();
slideObject.project_type_clips.push(clip_exp);
break;
case "f":
var clip_f = new type_f();
slideObject.project_type_clips.push(clip_f);
break;
case "oi":
var clip_oi = new type_oi();
slideObject.project_type_clips.push(clip_oi);
break;
case "tv":
var clip_tv = new type_tv();
slideObject.project_type_clips.push(clip_tv);
break;
}
} //end project_types loop
project_array.push(slideObject);
} // end object creation loop
loadProject();
}

Resources