Javascript 如果从服务中打开模式,则获取未定义的方法错误
我已经将引导模式放在服务内部,我想从那里打开它。 但我在firebug中遇到了错误。代码仍然可以正常工作,但firebug中存在错误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', [
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()