2016-09-22 131 views
0

我似乎無法將工廠中的對象傳遞給控制器​​。我知道這應該很簡單,但我很難過。角度工廠返回undefined

app.factory('test', [function(){ 
    var fac = {}; 
    fac.users = ['john', 'james', 'jake']; 
    return fac; 
}]); 

app.controller('dashboardCtrl', [ 
    '$scope', 
    'test', 
    function($scope, test){ 
     $scope.testFunction = function(){ 
      console.log(test.users); 
     } 
    } 
]); 

觸發$ scope.testFunction導致控制檯中記錄的「undefined」。

+0

何時何地激發'$ scope.testFunction'?它似乎工作得很好[JSFiddle](https://jsfiddle.net/4tgohyp3/2/) – Dan

+0

@PenVirus使jsfiddle和向我們展示。這應該很好。 –

回答

0

你有一個工作代碼示例。確保在DOM中引用了最新版本的Angular工廠/控制器文件,然後重試。