angularjs - Can't seem to be able to pass data from one controller to another -
the issue can't seem send information controller 1 controller 2... have service sets/gets data isn't working. actual error i'm getting controller 1's dataservice.getdata not function... when works elsewhere.
service 1 (in own file)
app.service('dataservice', function() { var data, queried; return { setdata: function(querydata) { this.data = querydata; this.queried = false; }, getdata: function() { this.queried = true; return this.data; } }; });
controller 1 (sending information)
app.controller('myctrl', ['$scope', '$location', '$state', function($scope, $location, $state, dataservice) { anotherservice.functionname(function(err, data) { // things here actualservice.doesntwork(function(err, data) { if (!err) { var query = {}; query.somefield = data.somefield; dataservice.setdata(query); $state.go("go.somewhere.else"); } }); }); }]);
controller 2 (getting information)
app.controller('myctrl2', ['$scope', '$location', '$state', function($scope, $location, $state, dataservice) { $scope.buttonpressed = function() { console.log(dataservice.getdata()); } }]);
you didn't injected service dataservice
inside myctrl
& myctrl2
, ensure dependency should injected before using it.
controller
app.controller('myctrl', ['$scope', '$location', '$state','dataservice', //<-added dependency here function($scope, $location, $state, dataservice) { anotherservice.functionname(function(err, data) { // things here actualservice.doesntwork(function(err, data) { if (!err) { var query = {}; query.somefield = data.somefield; dataservice.setdata(query); $state.go("go.somewhere.else"); } }); }); }]);
controller2
app.controller('myctrl2', ['$scope', '$location', '$state','dataservice',//<-added dependency here function($scope, $location, $state, dataservice) { $scope.buttonpressed = function() { console.log(dataservice.getdata()); } }]);
Comments
Post a Comment