AngularJS : Service for data between controllers -


i have page main controller , nested controller showing details product. want use a service in angular call server, retrieve data object , hold data object. main controller call service fetch data , details controller needs know updated , access data. service looks this:

.service("productservice", function ($http, $q) {     var product = {};     //interface returned     return ({         fetchproduct: fetchproduct,         clearproduct: clearproduct,         product: product     });      function fetchproduct(id) {         var request = $http({             method: "get",             url: "/online/productdata.ashx?itemid=" + id,             params: {                 action: "get"             }         });          return (request.then(handlesuccess, handleerror));     };      function clearproduct() {         product = {};     };      // transform error response, unwrapping application dta     // api response payload.     function handleerror(response) {          // api response server should returned in         // nomralized format. however, if request not handled         // server (or not handles - ex. server error),         // may have normalize on our end, best can.         if (             !angular.isobject(response.data) ||             !response.data.message             ) {             return ($q.reject("an unknown error occurred."));         }         // otherwise, use expected error message.         return ($q.reject(response.data.message));     };      // attempt transform successful response , unwrap application data     // api response payload.     function handlesuccess(response) {         product = response.data;         console.log("found data: " + angular.tojson(response.data))         return (response.data);     }; }) 

in main controller set scope object service this: $scope.selectedproduct = productservice;

when user clicks button show product called via $scope handle: $scope.selectedproduct.fetchproduct(id);

the details controller has same assignment $scope.selectedproduct. new using services understood angular bind service object , changes property product trigger binding updates. not happening - in fact see data after fetch operation. in service have console.log on returned data , showing correct data. product property not getting updated. can tell me doing wrong please? neither controller has access data after fetched. understand getting promise data never there after timeout check.

try factory instead of service.

angularjs: factory vs service vs provider


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

oracle - Changing start date for system jobs related to automatic statistics collections in 11g -