如果从服务打开模式,则获取未定义的方法错误

时间:2015-05-04 01:41:25

标签: javascript angularjs angular-bootstrap

我已将bootstrap模式放在服务中,我想从那里打开它。 但我在萤火虫中得到错误。代码仍然正常,但是firebug中有错误

我有这样的服务

'use strict';

angular.module('test')
    .factory('commonService', [
        '$http',
        '$log',
        '$modal',
        '$q',
        function ($http, $log, $modal, $q) {
            var api_url = "/api/";
            var
                commonService = {

                    self: this,

                    open: function (modelName, templateUrl) {

                        var modalInstance = $modal.open({
                            templateUrl: templateUrl,
                            controller: 'ModalInstanceCtrl',
                            resolve: {
                                modelName: function () {
                                    return modelName;
                                }
                            }
                        });

                        modalInstance.result.then(function (result) {
                            console.log(result);
                        }, function () {
                            $log.info('Modal dismissed at: ' + new Date());
                        });
                    }//

                };

            return commonService;
        }])
;

这是我的控制器

$scope.openModal = function (modelName) {
    commonService.open(modelName, 'test.html').then(function (result) {
        console.log('res')

    }, function (error) {
        console.log(error);
    })

};

我在模板中打开这样的

ng-click="openModal('User')"

问题是我在firebug中遇到此错误

TypeError: commonService.open(...) is undefined


commonService.open(modelName).then(function (result) {

但我的模态仍然可以打开内容

1 个答案:

答案 0 :(得分:1)

Common service.open方法应该返回modalInstance.result。 modalInstance.result是一个可以在控制器中解决它的承诺。