AngularJS Comma separated assignment using q.defer object - angularjs

Angular Django Registration Auth - djangoAuth.js
In lines 25 through 29, a comma separated assignment takes place. When you separate each line by semi-colons instead of commas, the code no longer works. Why is that?

and when you do that have you tried to put var before defining each variable? maybe it could be that. It seems to be a multiple declarative statement, then in order to make it work with semicolons you have to put it this way :
var deferred = $q.defer(),
var url = this.API_URL + args.url;
var method = args.method || "GET";
params = params;
var data = args.data || {};

Related

How can I access an element from an array?

var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1Ow_rhF3sibKL3OAQRXpK6LLZDjMOhW-5DKyWWiN5iZg/edit#gid=638513192');
var data = SpreadsheetApp.setActiveSheet(ss.getSheetByName('Graficos'));
Logger.log(SpreadsheetApp.getActiveSpreadsheet());
var ergo = data.getRange(3,2,4,1);//B3:B6 '
My guess to access the elements was to call var i = ergo[0]; but it didn't work. Do I have to declare ergo using a different syntax?
You need to use getValues() on the range, which will return a 2-dimensional array.
var ergo = data.getRange(3,2,4,1).getValues();
Also, you're not coding efficiently as you're essentially duplicating actions in your first two lines. Take a look at this refactoring:
function test() {
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1Ow_rhF3sibKL3OAQRXpK6LLZDjMOhW-5DKyWWiN5iZg/edit#gid=638513192');
var data = ss.getSheetByName('Graficos');
ss.setActiveSheet(data); // There doesn't seem to be a need to do this, so maybe delete this line
var ergo = data.getRange(3,2,4,1).getValues(); //B3:B6
}

How to convert dictionary to a string angularjs

I am currently working on my project using angularjs. I got everything already it is just that, i need to convert the dictionary list to a string separated by comma. I can only do this using python.
[{"name":"john"},{"name":"mark"},{"name":"peter"}]
I want to convert them to string
"john,mark,peter"
I would really appreciate your help. :)
.map and then .join will do
var array = [{"name":"john"},{"name":"mark"},{"name":"peter"}];
var names = array.map(function(item) {
return item.name;
}).join(',');
The map() method creates a new array with the results of calling a function for every array element. Use this to loop and then add that value to a variable.
var dict=[{"name":"john"},{"name":"mark"},{"name":"peter"}];
var string;
dict.map(function(value){
//do any stuff here
string+=value["name"]+",";
});
console.log(string);
Try map function to concatenate the values:
var dict=[{"name":"john"},{"name":"mark"},{"name":"peter"}];
var str="";
dict.map(function(a){
str+=a["name"]+",";
});
//feels ironical as question has AngularJS tag
document.getElementById("log").innerText=str;
<div id="log"></div>
You can simply iterate over each key-value pair and concat the extracted value with comma.
var obj = [{"name":"john"},{"name":"mark"},{"name":"peter"}]
var result = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
result += obj[p].name + ",";
}
}
result = result.replace(/,$/g,''); // to trim trailing comma

Parse url string object into host name in AngularJS

I have a scope variable that returns me an absolute url stored by the user. While displaying I would like to only show the host name for the given object.
for example.
$scope.url ="www.myurl.com/zyas?nxs"
i want to only display it as www.myurl.com.
How can I achieve this?
Have a look at the below code
string s = "www.myurl.com/zyas?nxs";
string newstr = s.split("/"); //newstr == "www.myurl.com"
You can do like this:
$scope.url ="www.myurl.com/zyas?nxs"
$scope.host = $scope.url.split('/')[0];
var hostName = $scope.url.split('/')[0];
I went with the normal split provided by JavaScript. Here's how I parsed it to the hostname and pushed it back to my JSON object.
$scope.blogposts = result.data
// console.log(urls);
$scope.blogposts.forEach(function(blog){
console.log(blog);
var a = blog.url.split('/')
// blog.push({'viewUrl':'a[0]'})
blog["viewUrl"] =a[0]
})
In a better way, you can do this without splitting and doing things
var url = new URL("http://www.myurl.com/zyas?dsfadf");
console.log(url.host); // gives the host name www.myurl.com

Convert String into Variable in AngularJS like in PHP variable variables

This is the outcome I am looking for:
$scope.donotShow = false;
The way I want to get it is in the controller in which I have one variable to work with:
var slug = "donot";
I would like to combine it with the string "Show":
var slugshow = slug + "Show";
From here I want the outcome I am looking for but this does not work:
$scope.slugshow = false;
How do I make the var slugshow show as "donotShow" thus giving me
$scope.donotShow = false;

Bug when sending array in node.js and socket.io

I use socket.io version 0.8.4
I have boiled down my problem to the following. I have data looking like this:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = [];
data.prop2["hey"] = "man";
I send the data from the server to the client this way:
socket.emit("data", data);
On the client side I receive the data this way:
socket.on("data", function(data){ console.log(data); });
The weird thing is:
data.prop1 = [];
data.prop1.push("man"); // This data exists in the client side data object
data.prop2 = [];
data.prop2["hey"] = "man"; // This data does not exist.
data.prop2 is just an empty array on the client side.
Is there a known bug in json serializing arrays on the form in prop2?
Thankyou in advance
EDIT:
Problem solved using this workaround:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // <= Object instead of array
data.prop2["hey"] = "man";
ECMA-262 about JSON.stringify:
The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Named properties are excluded from the stringification.
Arrays are supposed to have numerical property names. So when the data.prop2 is transformed to JSON (which socket.io sends the data in, I imagine), it doesn't get the 'hey' property. If you want to use non-numerical property names, you should use objects instead of arrays:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // Notice we're creating an object, not an array.
data.prop2["hey"] = "man"; // Alternatively: data.prop2.hey = "man"
Unfortunately, Javascript doesn't really work like that.
Check out this article, about half way down. It explains the problem where you try to set data.prop2["hey"] = "man";

Resources