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

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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值