Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

please look at this code. My button must call function checkResults() from controller CtrlTwo. How to do this?

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

function CtrlOne($scope){
    $scope.greet = 'Hi!  ';
}

function CtrlTwo($scope){
    $scope.$emit('checkResults', function(){
        alert('Function is called!');
    }
                );
}
<body ng-app="myApp">
    <div ng-controller="CtrlOne">
        {{greet}}
        <input type="submit" class="btn btn-primary pull-right" value="Check results" ng-href='#here' ng-click='checkResults()'/>
    </div>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</body>

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Here, hopefully this example will help you:

Javascript:

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

myApp.controller('CtrlOne', function($scope){
    $scope.greet = "hi!"
    var nTimes = 0;
    $scope.$on('myEvent', function(){
        $scope.greet = "Hi! The event has been called " + (++nTimes) + " times";
    });
});

myApp.controller('CtrlTwo', function($scope){
    $scope.emitCustomEvent = function(){ $scope.$emit('myEvent'); };
});

View:

<div ng-app="myApp">
    <div ng-controller="CtrlOne">
        {{greet}}
        <div ng-controller="CtrlTwo">
            <input type="button" value="Check results" ng-click="emitCustomEvent()" >
        </div>
    </div>
</div>

Working Example

share|improve this answer
1  
Thanks Josep. This answer helps to me. – Nebojisa Oct 15 '14 at 23:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.