0

I have 2 views,each one has a controller,I want to send data from View1 to View2 when I click on the "BL" Button:

this is View1: enter image description here

and this is View2: enter image description here

I tried to use the events my try,but the Controller2 will be activated only when I click on the "BL" Button I thought to use a service to send Data from controller1 to controller2,but I think it's not my case so please is there any help,how can I send data from View1 to View2 thanks

Edit: this is my second try:

View1/controller1:

 $scope.BLButton = function(){


    $scope.foo = Service.foo;
    Service.foo = '85';

    }
    .factory('Service', function() {
                                  var Service = {
                                        foo: '85'
                                      };
                                      return Service;
                                    });

View2/Controller2:

//this method is called to display the data in the input Text from a web Service
$scope.parentmethod = function() {
  //traitement
$scope.foo = Service.foo;
....
}

the problem with my code is that the method "parentmethod" of Controleller2 is never called when I click on the "BL" button

5
  • 2
    There are couple of ways to achieve this (shared service, components). Post your code for better help. Commented Jul 24, 2016 at 21:34
  • 3
    First thing you generally want to look at is using a service to share data across parts of the app. Your image is of little value, please provide minimal reproducible example Commented Jul 24, 2016 at 21:36
  • I have updated my post Sir Commented Jul 24, 2016 at 21:49
  • 1
    Wait, I'm all confused. I don't think you can attach a factory to a scope function, unless this is some syntax I'm not aware of. Commented Jul 24, 2016 at 21:58
  • I have called the name of Factory in both Controllers Sir Commented Jul 24, 2016 at 22:07

1 Answer 1

1

The best approach is to move all you business logic (BL) to a service which will maintain app state. You're trying to misuse controllers in a way, because their only goal is to glue presentation and BL and not to hold app state in any form. The problem is that controllers are created and destroyed on view change, so they don't fit as state holders. In your case you can store data in a service (which is instantiated and alive all the time your app works) and then retrieve it in another view in controller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.