remove slash from $location.url() in angular - angularjs

I need to get the GET params from url and pass it in the links to view in angular
I got from location.url() => "/?x=1&b=2"
but I need to get = > "?x=1&b=2" without slash
I tried to do that like the following:
var str = $location.url();
var x = str.replace(/\\/g, '');
but it kept the slash

var str = $location.url().substr(1);

Suggestion:
if you use route get the parameters with $routeParams
you can access the params like:
$routeParams.x
Answer to question:
Do it like a famous question suggests:
How can I get query string values in JavaScript?
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
To be used like:
var x = getParameterByName('x');

Related

AngularJS : How can i convert .properties file in to json object

I got a requirement to convert .properties file to JSON object using angular js.
I have no clue how to achive this, i searched in net but did not found the solution. Could you please help me with this.
.properties file contains
a.b.10=M
a.b.11=M50
a.b.12=M508
Output should be
{"a":{"b":{"10":"M","11":"M50","12":"M508"}}}
Try this
var str1 = "a.b.12=M508";
var str2 = "a.b.10=M";
var str3 = "a.b.11=M50";
var result = {};
function createObject(str) {
str.split('.').reduce((obj, key) => {
if (!obj[key] && !key.includes('='))
obj[key] = {};
else if (key.includes('=')) {
var keys = key.split('=');
obj[keys[0]] = keys[1];
}
return obj[key];
}, result);
return result;
}
createObject(str1);
createObject(str2);
createObject(str3);
console.log(result);

retrieving firebase database value in angularjs

screenshot of database structure
Auth.$onAuthStateChanged(function(authUser){
if(authUser){
console.log(authUser);
var ref = firebase.database().ref().child("users");
console.log("ref::" +ref);
//var rec = $firebaseArray(ref);
var usersRe= ref + "/" + authUser.uid + authUser.$id;
console.log(usersRe);
var userObj=$firebaseObject(usersRe);
$scope.currentUser=userObj;
}else{
$scope.currentUser='';
}
});
https://loginsystem-1ab0d.firebaseio.com/users/avL76HGunuYTG2fR3uO3OEblXZ33/
enable to get the child of userId(avL76HGunuYTG2fR3uO3OEblXZ33) which is the
key.
I think you're missing a slash and not using ref correctly. You don't append strings onto the ref, you would do a .child(...)
var usersRef = firebase.database().ref()
.child("users")
.child(authUser.uid + '/' + authUser.$id)

http.get attempts to retrieve image when parsing when I do not need it to

function getHTMLSource(url) {
return $http.get(url).then(function(response) {
var html = response.data;
url = getDetailPage(html)
return url ;
}
)};
getHTMLSource('http://www.footpatrol.co.uk/s:282524/search=282524/')
Whenever I attempt the above code, I get the following error message in my console GET http://localhost:9000/templates/footpatrol.co.uk/_assets/images/content/footpatrol_logo.png 404 (Not Found).
The image appears to exist at http://www.footpatrol.co.uk/templates/footpatrol.co.uk/_assets/images/content/footpatrol_logo.png but as I am running the scipt on localhost with Chrome's Allow-Control-Allow-Origin plugin, it appears to not play nicely. I do not want to GET the image I just want the source code, is there anyway around this?
UPDATE: I think it might be my parser causing the problem, because the error message is thrown here
function getDetailPage(html) {
var temp = document.createElement('div');
temp.innerHTML = html;
var a = temp.querySelector('a[class*=\'fp-product-thumb-link\']');
var partOfUrl = a.href;
var splitUrl = partOfUrl.split('/');
var url = 'http://www.footpatrol.co.uk/' + splitUrl[3] + '/' + splitUrl[4];
var url = 'http://www.footpatrol.co.uk/' + splitUrl[3] + '/' + splitUrl[4];
$log.debug('Detail page url found: ' + url);
return url;
}
Instead of creating an element, use the DOMParser API:
function getDetailPage(html) {
//var temp = document.createElement('div');
//temp.innerHTML = html;
var parser = new DOMParser();
var temp = parser.parseFromString(html, "text/html");
var a = temp.querySelector('a[class*=\'fp-product-thumb-link\']');
//var partOfUrl = a.href;
//console.log(partOfUrl);
//var splitUrl = partOfUrl.split('/');
//var url = 'http://www.footpatrol.co.uk/' + splitUrl[3] + '/' + splitUrl[4];
var elem = angular.element(a);
var url = 'http://www.footpatrol.co.uk/' + elem.attr('href');
console.debug('Detail page url found: ' + url);
return url;
}
Detail page url found: http://www.footpatrol.co.uk//footwear/282524-air-retro-15-obsidian.html

How to pass php response data from one page to another page in angularjs without using localstorage

i have one doubt how can i pass data from one page to another page in angularjs without using localstorage and data should exist even if user reload that page.
Is there any way out from here or not?
Thank's in advance
you can use cookie's :
https://docs.angularjs.org/api/ngCookies/service/$cookies
you can refer to this JSbin link for example:
http://jsbin.com/duxaqa/2/edit
Also, there is a nice stackoverflow answer that match your needs i think, with a more complete explanation than mine:
How to access cookies in AngularJS?
You can put data to cookie
// set cookie on one page:
setCookie("key", value);
// get cookie on another page:
var val = getCookie("key");
Using functions:
function setCookie(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires == "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires * 1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for (var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
}
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
But keep in mind that cookies size limited: 4093 bytes per domain.
you can pass data to another page using $rootScope or $cookieStore. But, only $cookieStore will save data when user reload the page. Take a look how to implement example:
angular.module('cookieStoreExample')
.controller('ExampleController', function($cookieStore) {
// Put cookie
$cookieStore.put('myFavorite','oatmeal');
// Get cookie
var favoriteCookie = $cookieStore.get('myFavorite');
// Removing a cookie
$cookieStore.remove('myFavorite');
});

Implementing pagination in backbone

I'm trying to implement a pretty basic pagination & filtering support with backbone
so far now, I have something like this on my Collection.url() function:
url: function() {
var url = 'http://localhost:9000/wines';
var query = '';
if (this.filter) query += '&filter=' + this.filter;
if (this.order) query += '&order=' + this.order;
if (this.page) query += '&page=' + this.page.toString();
if (this.len) query += '&len=' + this.len.toString();
if (query) url += '?' + query.substring(1);
return url;
},
you get the idea
the problem is that when I'm trying to create (POST) or update (PUT) an item, the querystring is also sent to the browser...
is there some way, in the url function, to detect the operation I'm issuing and add the querystring parameters only when fetching (GET) data?
or would you recommend another approach???
I found the following way to do it
I just override the fetch method, like this:
initialize: function(options) {
this._fetch = this.fetch;
this.fetch = function() {
options = {};
options.url = 'http://localhost:9000/wines'
data = {};
if (this.filter) data.filter = this.filter;
if (this.order) data.order = this.order;
if (this.page) data.page = this.page;
if (this.len) data.len = this.len;
options.data = data;
return this._fetch(options);
}
},
I couln't find a way to avoid saving the _fetch variable, I tried with
return Backbone.Collection.fetch(options);
return Backbone.Collection.prototype.fetch(options);
return Wines.fetch(options);
return Wines.prototype.fetch(options);
but none of them seems to work...
--
edit
found it on backbone page:
return Backbone.Collection.prototype.fetch.call(this, options);

Resources