Ajith
Ajith

Reputation: 393

How to access method in controller from another in angularjs

How can i access a method in tableController from my menuController. Here is my code.i want to call addRow() method from select() in menu controller. these controllers are in different modules.Please Help me.

     my menu controller
     var menuApp = angular.module('menuApp', []);
menuApp.controller('menuController', ['tableService', function ($scope, tableService) {
    $scope.menuItem = [
        {
            id: 1,
            title: "new",
            navigate:"N",
            child: [{
                id: 11,
                title: "new11",
                navigate: "N",
                child: [{
                    id: 12,
                    title: "new12",
                    navigate: "Y",
                    url:"/Home/index"
                }]
            }]
        },
        {
            id: 2,
            title: "new",
            child: [{
                id: 21,
                title: "new21",
                child: [{
                    id: 22,
                    title: "new22"
                }]
            }]
        }
    ];

    $scope.select = function (data) {
        if (data.navigate == "Y") {
            alert(data.url);
            tableService.add();
        }
    }
}]);

my table controller

     tableApp.controller('tableController', function ($scope, $rootScope,      $filter, $uibModal) {
    $scope.filteredPeople = [];

    $scope.currentPage = 1;
    $scope.pageSize = 10;
    $scope.people = [{ id: "1", name: "joe",disable:true },
                 { id: "2", name: "bill", disable: true },
                 { id: "3", name: "john", disable: true },
    { id: "1", name: "joe", disable: true },
                 { id: "2", name: "bill", disable: true },
                 { id: "3", name: "john", disable: true },
    { id: "1", name: "joe", disable: true },
                 { id: "2", name: "bill", disable: true },
                 { id: "3", name: "john", disable: true },
    { id: "1", name: "joe", disable: true },
                 { id: "2", name: "bill", disable: true },
                 { id: "3", name: "john", disable: true },
    { id: "1", name: "joe", disable: true },
                 { id: "2", name: "bill", disable: true },
                 { id: "3", name: "john", disable: true }];

    $scope.addRow = function () {
        debugger;
        $scope.people.unshift({
            id: "",
            name: "",
            disable:false
        });
        $scope.getPage();
    }

    $scope.getPage = function () {
        var begin = (($scope.currentPage - 1) * $scope.pageSize);
        var end = begin + $scope.pageSize;
        $scope.filteredPeople = $filter('filter')($scope.people, {
            id: $scope.idFilter,
            name: $scope.nameFilter

        });
        $scope.totalItems = $scope.filteredPeople.length;
        $scope.filteredPeople = $scope.filteredPeople.slice(begin, end);
    };
    $scope.getPage();

    $scope.pageChanged = function () {
        $scope.getPage();
    };
    $scope.open = function () {
        $scope.id = generateUUID();
    };
    $scope.dblclick = function (index) {
        for (var i = 0; i < $scope.filteredPeople.length; i++) {
            $scope.filteredPeople[i].disable = true;
        }
        return index.disable = false;
    }
    $scope.rowSelect = function (rowdata) {
        alert(rowdata.name);
    }
    $scope.openInput = function (index) {
        debugger;
        var modalInstance = $uibModal.open({
            templateUrl: '/Home/index',
            controller: 'testController',
            resolve: {
                items: function () {
                    return index;
                },
                cat: function () {
                    return 'Account';
                }
            }
        });
    }

});

Upvotes: 0

Views: 74

Answers (1)

AlainIb
AlainIb

Reputation: 4728

Example of a service shared between controllers & directives

/**
 * store and share data between controllers & directives
 */
angular.module('interfaceApp').service('shareData', function () {
    this.datas = [];

    this.add = function (data) {
        this.datas.push(data);
    };

    //retourne l'élément correspondant à la clé ( utilise la date comme clé )
    this.getElement = function (key) {
        ......
        return ;
    };

    this.getDatas = function () {
        return this.datas;
    };

});


/* Controller */
var searchModule = angular.module('searchModule', []);
   // inject the service in the controller
.controller('searchCtrl', function ($scope, shareData ) {

        shareData.add( ... );

        console.log( shareData.getDatas() );

});

A service is a singleton, so all controllers using it acces to the same datas ( when they call shareData.getDatas() )

Upvotes: 1

Related Questions