0

I have two controllers , a controller filter and a display controller results in the first I have a group of radio button , when you change the radio button , sending an event to change the value displayed in the second controller , the code is as follows : I know not coment changed immediately in the second controller

HTML :

      <div ng-controller="MyCtrl1">
    <input type="radio" ng-model="value" value="evol1" ng-change="setSelectedEvol(value)" name="evol">
        <br>
         <input type="radio" ng-model="value" value="evol2" ng-change="setSelectedEvol(value)" name="evol">

        </div>

<div ng-controller="MyCtrl2">
on change button radio evol = {{displayEvol}}
</div>

Angular:

var myApp = angular.module('myApp',[]);

    function MyCtrl1 ($scope) {

     $scope.setSelectedEvol = function (value) {

               $scope.selectedEvol = value;
                switch ($scope.selectedEvol) {
                    case 'evol1':
                        $scope.evol = 'Evol. VA';
                     break;
                    case 'evol2':
                        $scope.evol = 'Evol. %';
                       break;

                }

            $rootScope.$broadcast('ctr1Data', $scope.evol);

    }

    function MyCtrl2 ($scope) {

     $scope.$on('ctr1Data', function (event, args) {
        $scope.displayEvol= args;

    });


    }
2
  • you may need to use $rootScope.$on and its better if you create sharedService instead of directly using the controllers. Commented May 11, 2015 at 17:51
  • using $rootScope is not recommended... it's better to avoid using $rootScope.. if you want to share data between controllers then use services or make them inherited. Commented May 11, 2015 at 18:10

1 Answer 1

3

see this fiddle

http://jsfiddle.net/jigardafda/skyk3erh/

Using events

HTML

<div ng-app="myapp">
    <div ng-controller="MyCtrl1">
        <input type="radio" ng-model="value" value="evol1" ng-change="setSelectedEvol(value)" name="evol"/>
        <br/>
        <input type="radio" ng-model="value" value="evol2" ng-change="setSelectedEvol(value)" name="evol"/>
    </div>
    <div ng-controller="MyCtrl2">
        on change button radio evol = {{displayEvol}} 
    </div>
 </div>

JS

var myApp = angular.module('myapp',[]);

myApp
    .controller('MyCtrl1', function ($scope, $rootScope) {
        $scope.setSelectedEvol = function (value) {

            $scope.selectedEvol = value;

            switch ($scope.selectedEvol) {
                case 'evol1':
                    $scope.evol = 'Evol. VA';
                    break;
                case 'evol2':
                    $scope.evol = 'Evol. %';
                    break;          
            }

            $rootScope.$broadcast('ctr1Data', $scope.evol);

        };
    })
    .controller('MyCtrl2', function ($scope, $rootScope) {
        $scope.displayEvol = '';

        $scope.$on('ctr1Data', function (event, args) {
            $scope.displayEvol= args;

        });

    });

Using $parent

this will work if both controllers are side by side.

http://jsfiddle.net/jigardafda/skyk3erh/2/

HTML

<div ng-app="myapp">
    <div ng-controller="MyCtrl1">
        <input type="radio" ng-model="value" value="evol1" ng-change="setSelectedEvol(value)" name="evol"/>
        <br/>
        <input type="radio" ng-model="value" value="evol2" ng-change="setSelectedEvol(value)" name="evol"/>
    </div>
    <div ng-controller="MyCtrl2">
        on change button radio evol = {{displayEvol}}
        <br/>
        {{balue}}
        <br/>
        {{$parent.balue}}
    </div>
 </div>

JS

var myApp = angular.module('myapp',[]);

myApp
    .controller('MyCtrl1', function ($scope, $rootScope) {
        $scope.setSelectedEvol = function (value) {

            $scope.selectedEvol = value;

            switch ($scope.selectedEvol) {
                case 'evol1':
                    $scope.evol = 'Evol. VA';
                    break;
                case 'evol2':
                    $scope.evol = 'Evol. %';
                    break;          
            }

            $rootScope.$broadcast('ctr1Data', $scope.evol);

            $scope.$parent.balue = $scope.evol;
        };
    })
    .controller('MyCtrl2', function ($scope, $rootScope) {
        $scope.displayEvol = '';

        $scope.$on('ctr1Data', function (event, args) {
            $scope.displayEvol= args;

        });

    });
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.