假设我有一条路线:
angular.module('LiveAPP', ['ui.router',
'LiveAPP.main',
'LiveAPP.artist',
'liveAPP.signup',
'LiveAPP.factory',
'liveAPP.review'
])
.config(function($urlRouterProvider, $stateProvider) {
$stateProvider.state("home", {
url:"/",
templateUrl : '/home.html',
controller : 'mainCtrl',
resolve: {
artists: function () {
console.log('resolved')
return "I want this in controller";
}
}
})
})
在我的mainCtrl实例化之前,我希望运行绑定到艺术家的函数。所以在我的控制器中我有以下设置:
angular.module('LiveAPP.main',['LiveAPP.factory'])
.controller('mainCtrl', ['$rootScope','$scope','$http', '$location','dataFactory','artists', mainCtrl])
function mainCtrl($rootScope,$scope,$http,$location,dataFactory,artists){
console.log(artists) //expect the return from resolve in ui.router
}
当控制台记录艺术家将其评估为未定义时。任何人都知道为什么会这样?
答案 0 :(得分:0)
你使用ui-view吗?如果是,则不应该为mainCtrl设置任何ng-controller。我做了一些小调整,你的代码工作正常
angular.module('LiveAPP', ['ui.router', 'LiveAPP.main'])
.config(function($urlRouterProvider, $stateProvider) {
$stateProvider.state("home", {
url:"/",
templateUrl : 'home.html',
controller : 'mainCtrl',
resolve: {
artists: function () {
console.log('resolved');
return "I want this in controller";
}
}
});
});
angular.module('LiveAPP.main',[])
.controller('mainCtrl', ['$rootScope','$scope','$http', '$location', 'artists', mainCtrl]);
function mainCtrl($rootScope,$scope,$http,$location, artists){
console.log(artists); //expect the return from resolve in ui.router
}
<!DOCTYPE html>
<html lang="en" ng-app="LiveAPP">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div ui-view></div>
<script type="text/ng-template" id="home.html">
Lorem ipsum
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="http://rawgit.com/angular-ui/ui-router/0.2.15/release/angular-ui-router.js"></script>
<script src="index.js" type="text/javascript"></script>
</body>
</html>