javascript - Passing data between controllers using service and confusion with using [ '$scope' , 'service' , function($scope, service){}] -
I am stuck with this problem of passing data from second angle to this problem before my code: Whenever I click on the template controller's diwi, it will trigger the template with the help of ng-click ... Now my objective was to send the selected data of the responsible controller to send the template ...
Forums After reading here, I have been called 'Sales Service' Decided to create, so that I can transmit data between 2 controllers ...
// defined service proApp.service ('selectionService', function () {var selected_template; addTemplate = Function (newObj) {selected_template = newObj;}; getTemplate = function () {selected select_template;};}); // Controller 1 ... where the template is selected ProApp.controller ('TemplateController', function ($ radius, selection service) {$ scope.templates = [{template_name: "Template 1", template_text: "We interrupt Apologies for "}, {template_name:" Template 2 ", template_text:" Thank you for contacting us. "},},] // ng-click $ scope.setTemplate = function (tmp) {selectionService .addTemplate (tmp);}}); // Controller 2 ... is considered to hold the selected template. ProApp.controller ('Answer Controller', Function ($ Scope, Selection Service) {$ scope.template = selectionService.getTemplate ();}); So whenever I run this code, I get this error
No method is added to the object [object object]. Then I tapped the code where they were suggesting to use Squarebackets and put $ scope, serviceman and then write a function with the same parameters. I do not understand why I should do this? After making some changes like ['$ scope', 'service', function ($ scope, service) {}] , , I still can not figure out Am I solution to use data from another controller to pass the data.
Can you help? What am I missing? I am a lot new in the sense of JS.
I think that this is actually very simple to use you this By doing the need to add your methods to the service. Currently they are declared on window this ... using proApp.service ('selectionService', function) Change your service announcement for (var selected_atmate; this.addTemplate = function (newObj) {Selected_template = newObj;}; this.getTemplate = function () {selected select_template;};}); To use array notation for dependency, this is a good practice but it is not required. This will save you from headaches if you ever run your code through miniaturization.
Comments
Post a Comment