angularJS 依赖注入 controller 访问 factory返回数据undefined的问题【已解决】

it2022-05-09  24

controller访问factory时,不知时factory没返回还是controller没调用,运行后,浏览器没报错,但是没返回的数据,alert了一下显示undefined

这是JavaScript代码:

angular.module('invoiceApp',[]) .controller('invoice',['invoiceFac','$scope', function($scope,invoiceFac){ $scope.quantity = 1; $scope.cost = 2; $scope.inCur = 'USD'; $scope.currencies = invoiceFac.currencies; window.alert(invoiceFac.currencies);//测试 $scope.total = function (outCur){ return $scope.quantity * $scope.cost * invoiceFac.exchange($scope.inCur,outCur); }; $scope.pay = function(){ window.alert('Thanks!'); }; }]) .factory('invoiceFac', function(){ var rates = { 'USD': 1, 'EUR': 0.74, 'CNY': 6.09, }; return { currencies: ['USD','EUR','CNY'], exchange: function(inCur,outCur){ return rates[outCur]/rates[inCur]; } }; });

运行结果

翻遍了网上各大例子,我没看出问题在哪。。。黑人问号.jpg

                                                                                                                                                                            

我终于找到原因了,controller中的名称和调用顺序不能换orz。。。

.controller('invoice',['$scope','invoiceFac', function($scope,invoiceFac)

行吧,深刻教训(╬◣д◢)


最新回复(0)