1

I need to call a function in another controller from a link.

Now I'm in controller DashboardCtrl and I need to call function aggiornaListamedici() that is in controller AggiornaCtrl, how can I do that starting from a link??

This is my code now:

HTML

<div>
    <a href="#/aggiorna" ng-click="aggiornaListamedici()">
        <span ng-class="{button_download:true}"></span>
    </a>
</div>

ROUTING

medicoFvgAppModule.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/aggiorna', {templateUrl: 'partials/dashboard.html', controller: 'AggiornaCtrl'});
    $routeProvider.otherwise({redirectTo: '/dashboard'});
}])

CONTROLLER

medicoFvgAppControllers.controller('DashboardCtrl', ['$scope','$location','$rootScope',
    function($scope,$location,$rootScope) {
        // my controller dashboard
    }]);

medicoFvgAppControllers.controller('AggiornaCtrl', ['$scope', '$rootScope','$http', '$timeout', function($scope, $rootScope, $http, $timeout) {

    $scope.aggiornaListamedici = function(){
        // my function
    }
}]);
2
  • You shall use service for that Commented Feb 14, 2014 at 11:35
  • Can you explain me how to do this?? Commented Feb 14, 2014 at 11:37

1 Answer 1

2

You have two options here.

The most "right way" is to use a service that will do the job, and inject this service in both AggiornaCtrl and DashboardCtrl. If you're new to AngularJS you might want to read the official docs to know how to do it.

The other option is to make a common Controller that will be the parent of your two Controller, using controller inheritance, as described in this blog post. It is more a trick than an "official way of doing thing", but this hack has been proven very useful to me so far.

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.