JavaCake
JavaCake

Reputation: 4115

Calling controller functions within module

I have defined a function within my controller, which i want to call in a module. How is this possible?

The controller:

var App = angular.module('App',['ngResource','App.filters']);

App.controller('ExerciseCtrl', ['$scope','$http', function($scope, $http) {

    $scope.toggleOn = function (arr, id) {
        // Some code
    }; 

}]);

And the module:

angular.module('App.filters', []).filter('someFilter', [function () {
    return function () {
        someArray = [];
        toggleOn(someArray, 1); // Wish to call this function
    };
}]);

I want to call toggleOn within the module.

Upvotes: 1

Views: 54

Answers (2)

ppa
ppa

Reputation: 326

Off the top of my head, maybe something like this:

var App = angular.module('App',['ngResource','App.filters']);

App.service('toggleService', function(){
    var service = {
        toggleOn: function(arr, id){

        }
    };

    return service;
});

App.controller('ExerciseCtrl', ['$scope','$http', 'toggleService', function($scope, $http, toggleService) {

    $scope.toggleOn = toggleService.toggleOn;

}]);

angular.module('App.filters', []).filter('someFilter', ['toggleService', function (toggleService) {
    return function () {
        someArray = [];
        toggleService.toggleOn(someArray, 1); // Wish to call this function
    };
}]);

Upvotes: 1

Issam Zoli
Issam Zoli

Reputation: 2774

angular.module('App.filters', []).filter('someFilter', [function () {
    return function (scope) {
        someArray = [];
        scope.toggleOn(someArray, 1); // Wish to call this function
    };
}]);

And pass scope to filter

<span ng-controller="ExerciseCtrl">{{someFilter | this}}</span>

Upvotes: 0

Related Questions