javascript - AngularJS controller promise Then is not a function -


i having troubles making promise in controller.

plunker editor sample

this code module, controller , factory works fine less when callfactory, , wait response.

i got in console error "then not function" don't know wrong in code

angular     .module('sampleapp', [])     .controller('samplecontroller', samplecontroller)     .factory('myfactory', myfactory);  ////////// factory     function myfactory () {      var task = {       getdata : getdata,       counter:  0     };      return task;       // implementation details //     // ---------------------- //      function getdata () {       task.counter ++;       var response = "fakedata";       return response;     }   }  ////////// controller   function samplecontroller (myfactory, $log) {     $log.info("sample controller initialized");      var vm = this;     vm.title = "sampleview";     vm.callfactory = callfactory;     vm.factoryresponse;      callfactory();      // implementation details //     // ---------------------- //      function callfactory () {        myfactory.getdata()       .then(function (data) {         vm.factoryresponse = data;         return vm.factoryresponse;        })       .catch(function () {         $log.error("ups! cannot complete request :(");       });     }   } 

because factory method getdata return string. if want use then, method need return $promise. plunker:

////////// factory function myfactory ($q) {  var task = {   getdata : getdata,   counter:  0 }; var defer = $q.defer();  return task;   // implementation details // // ---------------------- // function getdata (randomparameter) {   task.counter ++;   var secondstring = ", sample factory value"   var response = randomparameter + secondstring;   defer.resolve(response);   return defer.$promise; }} 

more angular $q


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -