angularjs - Angular JS - Sending data from a directive to a parent controller -


i have app use ngcart directive in order store items added basket. problem directive has functionality of sending information items added user, need send information form.

so in order send in single object, need first extract data stored in directive main scope, , merge data form.

for need modify ngcart.js directive. tried make service, adviced here, don't get working. code added directive this

.service('ngcartdata', ['ngcart', function(ngcart){      return {         data:ngcart;     };   }]) 

, error saying module 'ngcart' not available!

i'm totally new services , factories in angular, don't know make work. made plunkr code (i tried modifying ngcart.js file code above, plunkr shows directive without modification). need able send data stored in directive in scope ngcart can listen in parent controller (see checkout section in plunkr).

any appreciated. thanks!

you had right idea in mind, , i'm surprised didn't work you.

i have edited app in following way (in script.js)

 app.controller('myctrl', function($scope, ngcart, mycart) {      $scope.names = [...];      ...      console.log(mycart.cart);  })     .factory('mycart',function(ngcart){         return {             cart: ngcart.$cart         };  }) 

and logged {shipping: 30, taxrate: null, tax: null, items: array[2]}, think need (i added 2 items before logged).

notice adding service redundant; data accessible whenever , wherever need. inject ngcart controller/service/etc. , updated data available you.

therefore, following code equivalent:

app.controller('myctrl', function($scope, ngcart) {        $scope.names = [...];        ...        console.log(ngcart.$cart);     }); 

a possible reason getting error got might that, while editing ngcart module, had sort of error (like typo), led ngcart being invisible angular.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)