I'm using Angular v1.3.10 and i'm setting a cookie successfully.
When i'm doing $cookieStore.put(key,value) and then $cookieStore.get(key) i see the object I added.
When i'm refreshing the page and doing $cookieStore.get(key) i get undefined.
Any idea why ?
At first appearance seems correct.
This works for me (version 1.3.0):
$cookieStore.put('MyVar', $scope.myVar);
// After refresh
$scope.myVar = $cookieStore.get('MyVar');
On the other hand, you can see in documentation that $cookieStore is deprecated. You must use $cookies. It's very similar.
Good luck!
Related
After a lot of research, and tinkering, I can't seem to actually get my Protractor test to do anything else other than have an Angular related error, even though I am using browser to avoid Angular being detected at all.
The test involves an Angular app, opening a dropdown in it, and clicking on the link for the console; the console opens a non-Angular admin page in a separate window.
So based on the many informative SO posts I found, I first used this...
browser.driver.getAllWindowHandles().then(function(handles) {
browser.driver.switchTo().window(handles[1]).then(function() {
//expect for new window here
});
});
Which appeared to work, as I could get to the window through repl pretty easily.
The issue is when either of the following were added...
browser.driver.getAllWindowHandles().then(function(handles) {
browser.driver.switchTo().window(handles[1]).then(function() {
expect(browser.getLocationAbsUrl()).toContain('/console/login.jsp');
expect(browser.driver.findElement(By.css('th.login')).getText()).toEqual('Login');
});
});
One expect check the URL and the other checks for the header element on the page, which is a table header. When I run this, I get the following:
Error while waiting for Protractor to sync with the page: "angular could not be found on the window"
When I decide to use browser.ignoreSynchronization = true, both in the function, or in a beforeEach, with or without a following afterEach setting it to false, I get the following:
JavascriptError: angular is not defined
I can't seem to get any "useful" errors to help me debug it, and trying it in repl does not help, as I get the same issue.
To be comprehensive, trying my URL expect without getting the second window will give me the root, and the other will fail.
Just doing one or the other will cause the same problem.
Changing to regular syntax (element(by.css...)) does not change things.
So much for my first question...
It appears that my use of browser.getLocationAbsUrl() is meant to be used for an Angular page, and was causing my issue...
Essentially, even though I believed I was using pure Webdriver calls, that call still required Angular on the page to work...
As stated in another post, the use of browser.driver.getCurrentUrl() is a non-Angular call using Webdriver, and fixed the problem. Thus, the final code is the following...
browser.sleep(1000); //to wait for the page to load
browser.driver.getAllWindowHandles().then(function(handles) {
browser.driver.switchTo().window(handles[1]).then(function() {
expect(browser.driver.getCurrentUrl()).toContain('/console/login.jsp');
expect(browser.driver.findElement(By.css('th.login')).getText()).toEqual('Login');
});
});
This works without setting ignoreSynchronization, BTW.
I realized it would probably be something relatively simple to fix it, just didn't expect I'd get it that quickly (I intended on submitting the question last night, but posted it this morning instead).
In any case, I hope this will at least be a good reference for anyone else facing the same issue.
Seems like getLocationAbsUrl is angular abs url.
Try using the native driver getCurrentUrl instead.
-- expect(browser.getLocationAbsUrl()).toContain('/console/login.jsp');
++ expect(browser.driver.getCurrentUrl() ...
I'm a beginner with AngularJs and i have some trouble understanding how to use $cookieStore :/
I have a lot of buttons. Everytime a button is clicked, a distinct function is called in the controller, and in this function, i'm trying to store a value in a cookie
example :
$cookieStore.put('cookie', '1');
And at the loading of the page, i added this line :
alert($cookieStore.get('cookie'));
When we load the page for the first time, it's normal to get an "undefined" popup. But the problem is, even after clicking multiple buttons, i always got an undefined popup after refresh.
Here's a working demo : http://plnkr.co/edit/6kuqaT7ISpo7uEwLHcZn?p=preview
Please help
Thanks
I checked your plunker cookie is setting properly :-)
Use STOP AND RUN button for refresh :P
I saw similar problem with refresh + $cookieStore some time ago, try using sessionStorage, if your target browser(s) allow that.
What Angular version do you use?
We are using DPS's PXPay to generate payments for our app.
On a successful transaction the url that is provided looks like
http://ourapp.com/checkout/complete?result=v5eX-DlMlfGe2FUcPeZgCFiKhZwgmCVhlTfL-rtnPszpe8S_6MYJMbVPDK14h7KxQM6xfKYUnXhz_RcSOHqw4VwHX97GU9XINHwh9DFt6aDHOLMvdAnSwZost5H2aOQlZgMESDzlwNGx_lfgeLba2Us41x437J3PWTJ_LLJVK4OY_7K4c6rxQ88Mve7FfoYd7P8CAZWxyHbC-wxCyLiocw4_NQ_34ct-IAsR1MM3C2OkPijfBAk-l72rOOMWGp8EoLpdOHyT0uITvsdjMvOFIHIMnSdVs2JFCslAmTRMDl4jkb5ezBf9wKZuo91KgII0kd9dFJMFCm7evt-X0ykLw_vjcgg85kZoRbDvi89nEGiWmFLVKM_xVgZYufeMMudOGRcHgi4i-RZJ4mml2JMzN2OtJKyQdpxuC-3b9KfnRbyy_F75Gak5fwJIf4tvo6VNJKUJuBtKYKafPyH_YJU4HQMeHP8uO0-FhMN3mzRCmGjvExuqx1BhA67R35pmaTbyxz-06JEvIqzf8FTftoBowLSeu37KtNmagZ8cJpMlCe4kUaFY4v6ZTx7h9rYx37oYtZNsSR1t3KDFzQYRs5LAg8RQ==&userid=OurDevId
This is definitely the request that is coming into the browser. BUT what is displayed in my browser URL bar... and what angular reads for the "result" token is this...
http://ourapp.com/checkout/complete?result=v5eX-DlMlfGe2FUcPeZgCFiKhZwgmCVhlTfL-rtnPszpe8S_6MYJMbVPDK14h7KxQM6xfKYUnXhz_RcSOHqw4VwHX97GU9XINHwh9DFt6aDHOLMvdAnSwZost5H2aOQlZgMESDzlwNGx_lfgeLba2Us41x437J3PWTJ_LLJVK4OY_7K4c6rxQ88Mve7FfoYd7P8CAZWxyHbC-wxCyLiocw4_NQ_34ct-IAsR1MM3C2OkPijfBAk-l72rOOMWGp8EoLpdOHyT0uITvsdjMvOFIHIMnSdVs2JFCslAmTRMDl4jkb5ezBf9wKZuo91KgII0kd9dFJMFCm7evt-X0ykLw_vjcgg85kZoRbDvi89nEGiWmFLVKM_xVgZYufeMMudOGRcHgi4i-RZJ4mml2JMzN2OtJKyQdpxuC-3b9KfnRbyy_F75Gak5fwJIf4tvo6VNJKUJuBtKYKafPyH_YJU4HQMeHP8uO0-FhMN3mzRCmGjvExuqx1BhA67R35pmaTbyxz-06JEvIqzf8FTftoBowLSeu37KtNmagZ8cJpMlCe4kUaFY4v6ZTx7h9rYx37oYtZNsSR1t3KDFzQYRs5LAg8RQ&userid=OurDevId
If you look closely... the two '==' are being taken off the end of the "result" token string.
I've tested in non angular applications and this isn't happening. Why does angular do this and how do I prevent it?
Angular cannot handle == because of its $location's search parsing. Just visit : https://angularjs.org/?foo==123 and the url will change to https://angularjs.org/?foo=
The same will not happen on a non-angular page.
Solution: Either patch angular OR HTML encode the section after ?result=
Depending on what you're trying to do with the URL that is returned, it might be simpler to just set window.location directly instead of working through Angular. This bypasses the query-string parsing in $location that's stripping the double equals.
I basically want to use tel tag to make a phone call.
<a class="callButton" href="tel: {{phoneno}}"></a>
I am using $compileProvider to remove unsafe tag which comes by default, It works perfectly fine post 1.0.4v of angularjs. However below this version it doesnt work. Can anyone suggest how to implement this feature using angular version 1.0.4 or below?
Here is the sanitizing code which I am using in js file
app.config(['$compileProvider', function ($compileProvider) {
$compileProvider.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/);
}
Assume I have already defined app variable.
P.S. This is an enhancement for particular app which basically works below 1.0.4v
Thanks
Add this to your scripts after angular is loaded.
I asume your on 1.1.5, but change to whatever you need the version to be.
http://code.angularjs.org/1.1.5/angular-sanitize.min.js
Example:
sanitizeSomething = function(string) {
return $sanitize(string);
};
sanitizedString = sanitizeSomething(string);
Using Restangular for AngularJS, keep getting an object object from Mongolab.
I'm sure it has to do with Promise but not sure how to use/implement this coming from old Java OO experience.
(Side note, would something like Eloquent Javascript, some book or resource help me understand the 'new' Javascript style?)
The small web app is for Disabled Students and is to input/edit the students, update the time they spend after school and then output reports for their parents/caregivers every week.
Here's the code that returns undefined when popping up a new form (AngularJS Boostrap UI modal)
I personally think Restangular & the documentation is a great addition so hope it doesn't dissuade others - this is just me not knowing enough.
Thanks in advance
app.js
...
$scope.editStudent = function(id) {
$scope.myStudent = Restangular.one("students", id);
console.log($scope.myStudent);
}
I'm the creator of Restangular :). Maybe I can help you a bit with this.
So, first thing you need to do is to configure the baseUrl for Restangular. For MongoLab you usually do have a base url that's similar to all of them.
Once you got that working, you need to check the format of the response:
If your response is wrapped in another object or envelope, you need to "unwrap" it in your responseExtractor. For that, check out https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case
Once you got that OK, you can start doing requests.
All Restangular requests return a Promise. Angular's templates are able to handle Promises and they're able to show the promise result in the HTML. So, if the promise isn't yet solved, it shows nothing and once you get the data from the server, it's shown in the template.
If what you want to do is to edit the object you get and then do a put, in that case, you cannot work with the promise, as you need to change values.
If that's the case, you need to assign the result of the promise to a $scope variable.
For that, you can do:
Restangular.one("students", id).get().then(function(serverStudent) {
$scope.myStudent = serverStudent;
});
This way, once the server returns the student, you'll assign this to the scope variable.
Hope this helps! Otherwise comment me here!
Also check out this example with MongoLab maybe it'll help you :)
http://plnkr.co/edit/d6yDka?p=preview