0

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

});
4
  • 2
    I think you can create a service with the fonctions you want to share between controllers & directives Commented Dec 22, 2015 at 12:55
  • @Alainlb i created a service named tableService. but that was not work for me. can u paste a example here? Commented Dec 22, 2015 at 12:57
  • 1
    It is not a good practice to call controller methods inside other controller. Like @Alainlb suggested is better if you create a separate service that you can inject in other components. This way your code is DRY, more modular and more flexible. Commented Dec 22, 2015 at 12:58
  • @Mirceac Thank you, How can i do it???? Commented Dec 22, 2015 at 12:59

1 Answer 1

1

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() )

Sign up to request clarification or add additional context in comments.

3 Comments

Alainlb , but i want to access method in another controller. i dnt want to get data , i am trying to push data.
shareData.add( ... );
@Alainlb Thank you. its work for me. but its works only when when i add var app =angular.module('interfaceApp',[]); app.service('shareData', function () { this.datas = []; ...................................... – Ajith 6 mins ago add a comment

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.