0

My code is like this

Controller.js
var errorMessage = { MessageHeader: "Error", Message: "Something went wrong. Please try again later." };
    angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', [
    '$scope',  'updateTruckNoService', 'messageModalService', '$modal',
    function ($scope,  updateTruckNoService, messageModalService, $modal) {

        messageModalService.showMessageModal(errorMessage);
    }
]);
Services.js
angular.module('RateRequestApp.services').factory('messageModalService', [ '$modal', messageModalService]);
function messageModalService($modal) {
    function showMessageModal(response) {
        var modalInstance = $modal.open({
            templateUrl: 'MessageModal.html',
            controller: 'ModalInstanceCtrl',
            resolve: {
                items: function () {
                    return response;
                }
            }
        });
    };
}

App.JS
angular.module('RateRequestApp', [
   'RateRequestApp.services',
   'RateRequestApp.controllers',
   'ui.bootstrap',
   'angular-loading-bar'
]);

Everything looks okay to me, But this throws an error

TypeError: Cannot read property 'showMessageModal' of undefined

at line

  messageModalService.showMessageModal(errorMessage);
0

2 Answers 2

2

The showMessageModal function is just defined in your messageModalService function. You're not returning it, so nobody can access it.

In fact, you're not returning anything, so your factory is producing undefined when it is invoked.

function messageModalService($modal) {
    function showMessageModal(response) {
        // ...
    }

    return {showMessageModal: showMessageModal};
}

or you can also just do:

function messageModalService($modal) {
    return {
        showMessageModal: function (response) {
            // ...
        }
    };
}
Sign up to request clarification or add additional context in comments.

Comments

1

It is simple that your service "messageModalService" is not returning the method 'showMessageModal', try to return form the methd messageModalService like below sample, it should work :)

return{
    showMessageModal: showMessageModal
};

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.