简体   繁体   English

角UIRouter解析未知的提供程序

[英]Angular UIRouter resolve unknown provider

I'm trying to use ui-router resolve to grab some data for use in the controller: 我正在尝试使用ui-router resolve来获取一些数据供控制器使用:

$stateProvider
    .state('home', {
    url: '/', 
    templateUrl: 'templates/home.html',
    controller: 'homeCtrl',
    resolve: {                          
       friends: ['$http', function($http) {
            return $http.get('api/friends.json').then(function(response) {
                    return response.data;
            })
        }]
    }
})

Here is the controller: 这是控制器:

angular.module('app').controller('homeCtrl', ['friends', function(friends) {
        this.friends = friends;
    }]);

HTML: HTML:

<section ng-controller="homeCtrl as home">
    <ul>
        <li ng-repeat="friend in home.friends">
            {{friend.name}} : {{friend.age}}
        </li>
    </ul>
</section>

How can I get this to work? 我该如何工作? The error I'm getting tells me define the service. 我收到的错误告诉我定义服务。 Not quite sure how to do that. 不太清楚该怎么做。 Also, I know I can get it to work by injecting $scope. 另外,我知道我可以通过注入$ scope使它工作。 I am hoping to avoid that, as I am trying to learn something new here. 我希望避免这种情况,因为我在这里尝试学习新知识。

You have included the controller twice. 您已两次包含控制器。 Once in the HTML, and once in the ui-router. 一次出现在HTML中,一次出现在ui-router中。 Therefore, you're actually getting two instances of the controller. 因此,您实际上得到了控制器的两个实例。

Solution: remove the controller include from the HTML, and add this to the ui-router $stateProvider: 解决方案:从HTML中删除控制器包含,并将其添加到ui-router $ stateProvider中:

controllerAs: 'home'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM