Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果从服务中打开模式,则获取未定义的方法错误_Javascript_Angularjs_Angular Bootstrap - Fatal编程技术网

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

Javascript 如果从服务中打开模式,则获取未定义的方法错误,javascript,angularjs,angular-bootstrap,Javascript,Angularjs,Angular Bootstrap,我已经将引导模式放在服务内部,我想从那里打开它。 但我在firebug中遇到了错误。代码仍然可以正常工作,但firebug中存在错误 TypeError: commonService.open(...) is undefined commonService.open(modelName).then(function (result) { 我有这样的服务 'use strict'; angular.module('test') .factory('commonService', [

我已经将引导模式放在服务内部,我想从那里打开它。 但我在firebug中遇到了错误。代码仍然可以正常工作,但firebug中存在错误

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


commonService.open(modelName).then(function (result) {
我有这样的服务

'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) {

但“我的模式”仍然可以打开,并且内容正常

公共服务。打开方法应返回modalInstance.result。这里的modalInstance.result是一个承诺,您可以在控制器中解决它。

哇,它工作得很好,但我不完全理解为什么。为什么错误会这样说。在我的案例中发生的事情modalInstance返回一个基于用户交互的值,但modalInstance不知道用户何时交互,因此它返回一个承诺对象,该对象可以在用户与您的modalInstance交互后解析。阅读$q文档。。在您的例子中,您在commonservice.open中不返回任何内容,因此当您调用方法common service.open().then()时,它类似于未定义的.then()