angularjs - Angular-service TypeError: Cannot read property 'saveURL' of undefined -


i trying write service store query string of url. doing storing in cookie , retrieving it. when try access saveurl method controller add query string, getting error:

typeerror: cannot read property 'saveurl' of undefined

controller.js

angular     .module("app.alerts.alertview")     .controller("alertviewscontroller", alertviewscontroller);      alertviewscontroller.$inject = [         "$scope", "$location", "alerthistory"     ];      function alertviewscontroller($scope, $location, alerthistory) {         alerthistory.saveurl($location.search());     }  

service.js

(function () {     "use strict";      angular         .module("app.alerts.alertview")         .service("alerthistory", alerthistory);      alerthistory.$inject = [         "$cookiestore"];      function alerthistory ($cookiestore) {         return {             saveurl: function (urlobj) {                 var array = $cookiestore.get("key");                 array.push(urlobj);                 $cookiestore.put("key", array);             }         };     } })(); 

i have injected service correctly. else can correct solve error. please help

what looks happening not returning think returning because return object not concrete enough js hang on to. also, forgot inject $location service. try rewriting follows:

angular     .module("app.alerts.alertview")     .service("alerthistory", [ '$cookiestore', function ($cookiestore) {     var x = {};     x.saveurl = function (urlobj) {         var array = $cookiestore.get("key");         array.push(urlobj);         $cookiestore.put("key", array);     };     return x; }]).controller('alertviewscontroller', [ '$scope', '$location', 'alerthistory', function ($scope, $location, alerthistory) {     alerthistory.saveurl($location.search()); }]); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -