0

in 1 controller I have a variable: $scope.text = 'text';

I click the button (in 1 controller). I open the window:

function clickDialog() {
    $mdDialog.show({
        controller: 'SecondController',
        templateUrl: 'path/second.html',
        parent: angular.element(document.body)                 
    });
}

in controller SecondController I click the button and I want to change the variable $scope.text = 'another text'; that is in 1 controller

How to do it?

Trevor Tracy
  • 356
  • 1
  • 10
609tom
  • 297
  • 1
  • 3
  • 14

1 Answers1

0

is it what you're trying to do: https://codepen.io/yonathanb/pen/XYPwQZ?editors=1010

Just use the:

  preserveScope: true,
  scope: $scope,

in your opener function

IsraGab
  • 4,819
  • 3
  • 27
  • 46
  • it works. but I do not know how to do it for 3 Controllers How to edit variable `$scope.text` is in the 3rd controller) after clicking the button (it is in 2 Controller). The button is in the `$mdDialog.show` window in 1 Controller – 609tom Jul 03 '18 at 14:49
  • I don't know what you are trying to do. In your question it wasn't about a third controller. My answer is relative to your question. Why do you have a third controller? Also, If you want us to find out your issues, please at least accept the answer. – IsraGab Jul 03 '18 at 18:27
  • My question 2, is under your answer. I do not know how to pass variable values between controllers. In 1 controller I have a window: `$mdDialog.show({ controller: 'SecondController', templateUrl: 'path/second.html', parent: angular.element(document.body) });` which has a button in the 2nd `SecondController` controller. After clicking the button, I want to change the value of the variable in the 3rd controller. I do not know how to do this? – 609tom Jul 03 '18 at 19:00
  • In order to pass value between controllers in angularjs you have 2 options. 1) set the value to a parent controller (like $rootscope). 2) set the value into a service/factory then insert that value into all the controllers (best practice). refer to that answer: https://stackoverflow.com/questions/15123215/data-binding-between-pages-in-angularjs?rq=1 – IsraGab Jul 03 '18 at 19:04