0

I have a controller as HomeController in my home.html page and I want to call the same controller and its contents in my about.html page. The below code is for Home page which will show the First name and Last name of the user and I want to call the same functionality like call the first name and last name in the about us page. How to do that? As of now I have just copied the whole and pasted in the about controller just changing the controller name from HomeController to AboutController.

(function () { 'use strict';

angular
    .module('app')
    .controller('HomeController', HomeController);

HomeController.$inject = ['UserService', '$rootScope','$scope', '$http'];
    function HomeController(UserService, $rootScope,$scope, $http) {
    var vm = this;

    vm.user = null;
    vm.allUsers = [];
    vm.deleteUser = deleteUser;

    initController();

    function initController() {
        loadCurrentUser();
        loadAllUsers();
    }

    function loadCurrentUser() {
        UserService.GetByUsername($rootScope.globals.currentUser.username)
            .then(function (user) {
                vm.user = user;
            });
    }

    function loadAllUsers() {
        UserService.GetAll()
            .then(function (users) {
                vm.allUsers = users;
            });
    }

    function deleteUser(id) {
        UserService.Delete(id)
        .then(function () {
            loadAllUsers();
        });
    }
    $http.get('webservice').
        then(function(response) {
        $scope.user_message = response.data.data;
    });
}

})();

4
  • If I understand your question correctly, this is possible. Please elaborate your question. Show code structure. Commented Mar 7, 2017 at 12:19
  • You can use controller inheritance. Have a common functionality in one base controller and then in other controllers you can initiate the base scope with the derived controller scope so that you can access base methods from view. Commented Mar 7, 2017 at 12:34
  • You can use (not call) the same controller wherever you want by indicating it in each state of your router where you want to use it. Commented Mar 7, 2017 at 12:35
  • I have posted my code and elaborated exactly what I am trying do. Please help me with any example so that I will understand the structure properly. Thanks in advance. Commented Mar 9, 2017 at 6:24

1 Answer 1

0

If you are trying to use the same controller for two different pages, yes it is possible.

But note that an instance of the controller will be created for each, if you are trying to have common data/methods between controllers, you are looking for services.

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

1 Comment

Can you please show me any example, I have posted my code above.

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.