我正在尝试使用toastr获取成功和错误消息,我想将它放在工厂中,但我得到的是TypeError,没有任何细节。 错误消息
TypeError: object is not a function
at angular.js:8113
at deferred.promise.then.wrappedCallback (angular.js:11573)
at angular.js:11659
at Scope.$get.Scope.$eval (angular.js:12702)
at Scope.$get.Scope.$digest (angular.js:12514)
at Scope.$get.Scope.$apply (angular.js:12806)
at done (angular.js:8379)
at completeRequest (angular.js:8593)
at XMLHttpRequest.xhr.onreadystatechange (angular.js:8532)angular.js:10072 (anonymous function)
厂
'use strict';
myApp.factory('notificationFactory',
function () {
var logIt;
toastr.options = {
"closeButton": true,
"positionClass": "toast-bottom-right",
"timeOut": "3000"
};
logIt = function (message, type) {
return toastr[type](message);
};
return {
success: function (message) {
logIt(message, 'success');
},
error: function (message) {
logIt(message, 'error');
}
};
}
);
控制器
$scope.EmailPdfNew = function () {
var id = $scope.newCivil.CivilCaseId
$http.get('/api/PdfCivil/' + id)
.success(function () {
$http.post('/Home/EmailPdf')
.success(notificationFactory)
.error(notificationFactory);
});
}
更新
'use strict';
myApp.factory('notificationFactory',
function (toastr) {
var logIt;
toastr.options = {
"closeButton": true,
"positionClass": "toast-bottom-right",
"timeOut": "3000"
};
logIt = function (message, type) {
return toastr[type](message);
};
return {
success: function (message) {
logIt(message, 'success');
},
error: function (message) {
logIt(message, 'error');
}
};
}
);
将toastr注入函数会在页面加载时产生此错误
Error: [$injector:unpr] Unknown provider: toastrProvider <- toastr <- notificationFactory
errors.angularjs.org/1.2.26/$injector/unpr?p0=toastrProvider%20%3C-%20toastr %20%3C-%20notificationFactory
答案 0 :(得分:4)
确保注入模块依赖关系和功能。
<强> Plunker 强>
angular.module('plunker', ['toastr']);
...
app.factory('notificationFactory', function(toastr) {