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");
        });


})
13
  • 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 stackoverflow.com/questions/32486871/… Commented Dec 12, 2018 at 5:25
  • Its ok if im able to do this localStorage.setItem("lastname", index); but I'm not able to add anything here Commented Dec 12, 2018 at 5:29
  • Do you mean that you are able to do localStorage.setItem("lastname",index) but nothing else? Commented Dec 12, 2018 at 5:35
  • Im not able to add anything Commented Dec 12, 2018 at 5:38
  • I want to add this localStorage.setItem("lastname",index) Commented Dec 12, 2018 at 5:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Im not passing it from anywhere!
I just want to store the value localStorage.setItem("lastname", "Smith"); smith in a variable lastname in local Storage or session storage
with out passing how controller knows about localstorage.then compile time it gives error unknown function unable to proceced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.