0

Im getting following error when I add localStorage.setItem("lastname", "Smith"); line to the controller.

Error: Unspecified error. at $scope.singleSg (file:///C:/ashwitha%20G%20S/sugg/js/controller.js:28:21) at fn (Function code:2:296) at expensiveCheckFn (file:///C:/ashwitha%20G%20S/sugg/lib/angular.js:15906:11) at callback (file:///C:/ashwitha%20G%20S/sugg/lib/angular.js:25885:17)
at Scope.prototype.$eval (file:///C:/ashwitha%20G%20S/sugg/lib/angular.js:17682:9) at Scope.prototype.$apply (file:///C:/ashwitha%20G%20S/sugg/lib/angular.js:17782:13) at Anonymous function (file:///C:/ashwitha%20G%20S/sugg/lib/angular.js:25890:17) at n.event.dispatch (https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js:3:12312) at r.handle (https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js:3:9090)

.controller('sgController', function ($scope, $state, loginOperation) {

    loginOperation.suggestion().success(function (recData) {
        if (recData) {
                //console.log(recData);
            $scope.suggestions = recData;
            $scope.singleSg = function (index) {
                console.log(index);
                localStorage.setItem("lastname", "Smith");
                $state.go('sugRm');
            }
        }
    }).error(function () {
        console.log("Request failed");
        });


})
Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
  • 1
    Have you injected localstorage service into your controller and missed that in here? How are you getting localstorage? See the implementation in details at https://stackoverflow.com/questions/32486871/angularjs-use-local-storage – PM. Dec 12 '18 at 05:25
  • Its ok if im able to do this localStorage.setItem("lastname", index); but I'm not able to add anything here –  Dec 12 '18 at 05:29
  • Do you mean that you are able to do `localStorage.setItem("lastname",index)` but nothing else? – PM. Dec 12 '18 at 05:35
  • Im not able to add anything –  Dec 12 '18 at 05:38
  • I want to add this `localStorage.setItem("lastname",index)` –  Dec 12 '18 at 05:38
  • Looks like issue with the localStorage service, if you refer the link in my comment, you will see two ways to get the localStorage, one is to get it from $window, but then you will have to configure it everytime, whereas second approach looks better, you need to set it once and then pass it everywhere as a service. Refer the accepted answer. – PM. Dec 12 '18 at 05:42
  • Tried with `window.localStorage.setItem('key', 'value' );` method, it gives the same error. I just want to save the index value to local storage and route to other state. If I comment this line `localStorage.setItem("lastname", "Smith"); ` it is routing to the state (`$state.go('sugRm');`) –  Dec 12 '18 at 05:52
  • Can you create a Plunker(https://plnkr.co/edit/?p=catalogue) or something and show your code? – PM. Dec 12 '18 at 05:57
  • Your code from github works for me. I don't see any error in console other than issue with favicon.ico. What browser are you using? I tried it on Chrome Version 70.0.3538.110 (Official Build) (64-bit) – PM. Dec 12 '18 at 22:26
  • Check the ReadMe.MD of your repo. – PM. Dec 12 '18 at 22:38
  • I'm using Microsoft Edge 42.17134.1.0, hope you checked suggestion page and clicked read more. –  Dec 14 '18 at 05:02
  • It works for me on Edge(Microsoft Edge 42.17134.1.0) as well, on my local environment I went to the Suggestion page, there I can see the list of items. Clicked on "Read More" button and saw the details of that item. When I click on an item, I can see the id of that item against "lastname" in the local storage, and it get overwritten when I click on something else. I think you have issue somewhere else. – PM. Dec 14 '18 at 05:58
  • when i click on the read more, index is getting printed in console, then I'm getting that error. –  Dec 14 '18 at 09:07

1 Answers1

0
.controller('sgController', function ($scope, $state,$localStorage,loginOperation) {

loginOperation.suggestion().success(function (recData) {
    if (recData) {
            //console.log(recData);
        $scope.suggestions = recData;
        $scope.singleSg = function (index) {
            console.log(index);
            $localStorage.setItem("lastname", "Smith");
            $state.go('sugRm');
        }
    }
}).error(function () {
    console.log("Request failed");
    });

})

use $localStorage as parameter in controller Function then only You get localstorage Object.

Raju
  • 459
  • 5
  • 23
  • Im not passing it from anywhere! –  Dec 12 '18 at 08:37
  • I just want to store the value `localStorage.setItem("lastname", "Smith");` smith in a variable lastname in local Storage or session storage –  Dec 12 '18 at 08:38
  • with out passing how controller knows about localstorage.then compile time it gives error unknown function unable to proceced. – Raju Dec 12 '18 at 08:39