解决Angularjs ui-router问题
angularjs ui-router issue with resolve
我正在创建一个应用,使用angularjs和angular-ui-router进行基于状态的路由。我的应用程序中有几条路线,但其中一条需要注意。状态定义如下
state('profile', {
url: '/profile',
controller: 'ProfileController', //controller on a different file
resolve: {
user_profile: ['User', function (User){
return User.getProfile(); //returns a json object
}]
},
templateUrl: 'profile.html'
})
getProfile代码:getProfile:函数(){Var = $q.defer();
$http.get('/profile')
.success(function(res){
if (res.status == 'success'){
d.resolve(res);
} else {
d.reject();
}
})
.error(function(reason){
d.reject(reason);
});
return d.promise;
}
现在,在我的控制器文件中,我有以下代码:
.controller("ProfileController", ['$scope',function($scope,user_profile){
console.log(user_profile);
}]);
问题是resolve应该在一个名为user_profile的参数上注入解析的结果(因为在状态定义的键中我使用了user_profile),并且注入的依赖项有一个由console.log调用声明的undefined值。
我已经调试了我的代码,并且在服务器上获取用户配置文件的服务工作得很好,所以,在那里没有问题。即使我在user_profile解析键上使用对象返回,错误仍然存在。
另一件事是,如果我在状态定义中设置了控制器属性为状态定义中定义的函数,那么依赖项就会被注入正确的值,但我不希望这样,我希望依赖项也注入到已定义的控制器上,而不是内联控制器上。
那么,这里会发生什么呢?
提前感谢任何答案。
您正在使用angular的严格DI语法(依赖项名称的字符串数组,函数作为数组的最后参数),但您没有在数组中指定user_profile
依赖项。
。
.controller("ProfileController", ['$scope','user_profile',function($scope,user_profile) { console.log(user_profile); }]);
相关文章:
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- Angular JS/UI Router:为给定状态禁用指令
- 使用AngularJS中的$stateProvider和ui.router
- 带有ui.router的带角度的嵌套视图
- 如何使用AngularJS,Vise和UI Router全局实现身份验证
- 如何使用AngularJS和ui-router从Laravel 5.1获取下载文件
- AngularJS - ui.router - 如何在动态添加它们后重定向到所需的状态
- Angular UI Router-在不了解第二级嵌套视图的情况下更改第三级嵌套视图
- angularjs+ui.router:指令控制器和链接函数之间的不同行为
- AngularJS : ui.router 不显示模板
- AngularJS中存在与$scope变量相关的问题.UI中的值未更改
- [AngularJS][UI Router]在视图中显示视图
- Angular UI Router Resolve在AJAX完成之前未阻止状态更改
- 隐藏空模板,直到使用Restangular和UI Router加载数据
- 加载视图以查看问题 - AngularJS + ui.router
- 使用 AngularJS ui-router 深度链接到 URL 的问题
- 解决Angularjs ui-router问题
- ui-router动态路由问题
- ionic框架中调用视图的Ui-router问题
- $rootScope和UI-Router Resolve不持久的问题