angularjs controller控制器流入对象顺序

angularjs controller控制器注入对象顺序

 

angularjs在controller中注入server的时候,需要注意注入服务的顺序,否则会出现server没有自己定义的功能方法的奇怪现象。

 

错误案例:

 

//选择支付类型
.state('selectCashType', {
	url: '/detailsPayIncom/selectCashType',
	templateUrl: 'detailsPayIncom/selectCashType.html',
	controller: ["$scope",  "$state", function ($scope, $http ,$state) {
		$scope.jumpTo = function(){
			$state.go("getCashByZhifubao");
		}
	}]
})

 

$scope  和 $state 分别是数组的第0个 和 第1个值,那么最后的controller函数的参数必须要要与之一一对应。

上述例子中,函数中的$http实际上是前面数组第1个值$state的实例。

 

 

解决办法:

 

//选择支付类型
.state('selectCashType', {
	url: '/detailsPayIncom/selectCashType',
	templateUrl: 'detailsPayIncom/selectCashType.html',
	controller: ["$scope",  "$state", function ($scope, $state, $http) {
		$scope.jumpTo = function(){
			$state.go("getCashByZhifubao");
		}
	}]
})

 

将数组前面注入的server字符串 与 最后一个函数的参数顺序一一对应。