1

我已经创建了通知工厂并将其传递给控制器​​,内部控制器将工厂分配给出错的范围。为什么工厂无法访问控制器

alertsManager

MyApp.factory('alertsManager', function() { 
       return { 
        alerts: {}, 
        addAlert: function(message, type) { 
         this.alerts[type] = this.alerts[type] || []; 
         this.alerts[type].push(message); 
        }, 
        clearAlerts: function() { 
         for(var x in this.alerts) { 
         delete this.alerts[x]; 
        } 
        } 
       }; 
      }); 



    var LoginController = function($scope,$rootScope,alerts,alertsManager) 
     { 
     $scope.alerts = alertsManager.alerts; 
     // getting error. 
     **angular.js:11594 TypeError: Cannot read property 'alerts' of undefined** 
     } 

     LoginController.$inject = ['$scope', '$rootScope','alerts','alertsManager']; 


     **why factory not able to access inside controller.* 
+0

您正在使用$ inject注入4。但是,3在函数'LoginController'中。并删除'在功能。 – SaiUnique

+0

你有'警报'工厂吗? – SaiUnique

+0

是的,对于所有工厂都是一样的,我无法访问控制器内部的工厂功能。 –

回答

0

无需注入 '提醒',在控制器的依赖。

0

对不起..很愚蠢的问题..你确定你在Index.html中包含这些文件吗?

这样的:

<script src="app/services/alertsManager.js"></script> 
1

尝试类似下面。

代码:

var myApp = angular.module('myApp', []); 

myApp.factory('alertsManager', function() { 
    return { 
    alerts: {'alert':"i'm from factory service"}, 
    addAlert: function() { //code }, 
    clearAlerts: function() { //code } 
    } 
}); 

myApp.controller('MyCtrl',['$scope','alertsManager', function($scope, alertsManager) { 
    $scope.test = alertsManager.alerts.alert; 
}]); 

注:进样厂家售后服务到控制器

工作样本here