2014-11-15 3 views
1

Я пытаюсь использовать 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

Я могу ошибаться, но, похоже, вам просто нужно вводить его 'функцию ('toastr') {' – Dylan

+0

, в которой функция? – texas697

+0

В вашем заводе – Dylan

ответ

4

Просто убедитесь, что вы ввели свой модуль в зависимость и функцию.

Plunker

angular.module('plunker', ['toastr']); 

... 

app.factory('notificationFactory', function(toastr) { 
Смежные вопросы