RequireJS和AngularJS。
如果我尝试访问服务中的控制器,我会收到错误。
论点' onCloseAlert'不是一个功能,未定义。
此服务由其他控制器(其他文件)调用。
服务的代码。
(function( define ) {
"use strict";
define([],
function() {
var AlertService = function( $modal, $log ) {
var onShowAlert = function( alertTitle, alertMessage ) {
var modalInstance = $modal.open({
templateUrl : 'app/moduloUtils/viewmodel/alert.html',
controller : 'onCloseAlert'
});
return {
alertTitle : alertTitle,
alertMessage : alertMessage
};
};
var onCloseAlert = function( $modalInstance ) {
$modalInstance.close();
return true;
};
return {
showAlert : onShowAlert,
closeAlert : onCloseAlert
};
};
return [ "$modal", "$log", AlertService ];
}
);
}( define ));
谢谢
答案 0 :(得分:0)
要将控制器指定为字符串,必须在angular.controller('onCloseAlert', ...)
注册,或者必须是全局功能(并使用$controllerProvider.allowGlobals()
启用) 。在您的情况下,onCloseAlert
是作用域中定义的名称,因此仅将该名称引用为:
var modalInstance = $modal.open({
templateUrl : 'app/moduloUtils/viewmodel/alert.html',
controller : onCloseAlert // no quotes, just reference the local
});
我建议使用完整的#34;注释"对于控制器:
var onCloseAlert = ['$modalInstance', function( $modalInstance ) {
$modalInstance.close();
return true;
}];
答案 1 :(得分:0)
如果我像服务一样调用模态显示ok,但HTML不调用closeAlert。
<div>
<button class="btn btn-sm btn-primary pull-right m-t-n-xs" ng-click="closeAlert();"><strong>Fechar</strong></button>
</div>
如果我改变并使用控制器也不行。 :(
模块
(function ( define, angular ) {
"use strict";
define([
'modAute/services/Autenticacao',
'modAute/controllers/LoginController',
'modUtil/controllers/AlertController'
],
function (LoginController, AlertController ) {
var nomeModulo = "Autenticacao";
angular
.module( nomeModulo, [] )
.service( "autenticacao", Autenticacao )
.controller( "LoginController", LoginController )
.controller( "AlertController", AlertController );
return nomeModulo;
});
}( define, angular ));
LoginController
(function( define ) {
"use strict";
define([
'modUtil/controllers/AlertController'
],
function ( AlertController ) {
var LoginController = function( autenticacao, $scope, $q, $log, $location ) {
...
var onForgot = function() {
return autenticacao
.forgot($scope.email)
.then(function onResult_forgot( response ) {
AlertController.showAlert('Atenção', '..');
return true;
}
};
$scope.forgotPas = onForgot;
};
return ["autenticacao", "$scope", "$q", "$log", "$location", LoginController ];
}
);
}( define ));
AlertController
(function( define ) {
"use strict";
define([],
function() {
var AlertController = function( $scope, $modal, $log ) {
var onShowAlert = function( alertTitle, alertMessage ) {
$scope.alertTitle = alertTitle;
$scope.alertMessage = alertMessage;
var modalInstance = $modal.open({
templateUrl : 'app/moduloUtils/viewmodel/alert.html',
controller : 'onCloseAlert'
});
};
var onCloseAlert = [ '$modalInstance', function( $modalInstance ) {
$modalInstance.close();
return true;
}];
$scope.showAlert = onShowAlert;
$scope.closeAlert = onCloseAlert;
};
return [ "$scope", "$modal", "$log", AlertController ];
}
);
}( define ));
LoginController上的第157行错误 AlertController.showAlert('Atenção','..');
TypeError:undefined不是函数 at onResult_forgot(http://lecoleco.desenv/sysAdmin/app/moduloAutenticacao/controllers/LoginController.js:157:53)