Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $stateProvider中的服务_Angularjs - Fatal编程技术网

Angularjs $stateProvider中的服务

Angularjs $stateProvider中的服务,angularjs,Angularjs,我试图使用resolve并将其注入工厂,然后在控制器中打印解析返回的数据 基本上,代码看起来很像: angular .module('appRouter',['ui.router']) .controller('routerCtrl',function(resolveData){ console.log(resolveData); }) .factory('repoService',function($h

我试图使用
resolve
并将其注入
工厂
,然后在
控制器
中打印解析返回的数据

基本上,代码看起来很像:

 angular
        .module('appRouter',['ui.router'])
        .controller('routerCtrl',function(resolveData){
            console.log(resolveData);

        })
        .factory('repoService',function($http){
            var API = "https://jsonplaceholder.typicode.com/posts/1";
            return {
                getResolveData : function($http){
                    return $http.get(API);
                }
             }
        })
        .config(function($stateProvider,$urlRouterProvider){
            $stateProvider
                .state('settings.profile',{
                    url:'/profile',
                    templateUrl:'templates/profile.html'
                })
                .state('settings.account',{
                    url:'/account',
                    templateUrl:'templates/account.html',
                    controller:'routerCtrl',
                    controllerAs:'vm',
                    resolve:{
                        resolveData : function(repoService){
                            return repoService.getResolveData().then(function(response){
                                return response.data;
                            });
                        }
                    }
                });

            $urlRouterProvider.otherwise('/settings/profile');  

        });
但代码不起作用<代码>控制台中无错误,但路由到设置/帐户不起作用。控制台上没有打印任何内容。

但是

如果我们不注入服务,下面的代码就可以很好地工作

            .state('settings.account',{
                url:'/account',
                templateUrl:'templates/account.html',
                controller:'routerCtrl',
                controllerAs:'vm',
                resolve:{
                    resolveData : function(){
                         var root = 'https://jsonplaceholder.typicode.com';
                         return $http.get(root+'/posts/1').then(function(response){

                             return response.data;
                         });
                    }
                }
            });

我不知道我做错了什么?请解释。

getResolveData:function($http){
remove$http它已经在类级别上定义了。@Walfrat可爱。谢谢。