无法将解析数据注入我的控制器

时间:2013-06-25 20:54:50

标签: angularjs

这是我的路线:

$routeProvider.when('/blog/bearbeiten/:id', {
    templateUrl: 'adminpages/blog/beitragbearbeiten',
    controller: 'blogEditPostController',
    resolve: {
        loadData: function($q, $route, blog) {
            var defer = $q.defer();
            var post = $route.current.params.id;

            var fn = function(result) {
                defer.resolve(result);
            };
            blog.getPost(post, fn);
            return defer.promise;
        }
    }
});

这是我的控制者:

app.controllers.controller('blogEditPostController', function(loadData) {

});

这是博客服务:

app.services.factory('blog', function ($http) {
  return {
    getPosts: function(page, fn) {
        $http.get('/api/posts/' + page).
            success(function(data) {
                fn(data);
            });
    },
    getPost: function(id, fn) {
        $http.get('/api/post/' + id).
        success(function(data) {
                console.log(data);
                fn(data);
        });
    }
  };
});

调用defer.resolve函数,加载模板,结果变量的内容正确;但是我得到了这个错误:

  

错误:未知提供者:loadDataProvider< - loadData       在错误()       在http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2765:19       at Object.getService [as get](http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2891:39)       在http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2770:45       at getService(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2891:39)       在调用(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2909:13)       在Object.instantiate(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2943:23)       在http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:5368:28       在http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:4937:17       at forEach(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:155:20)   angular.js:6349

当我在收到上述错误后尝试再次加载视图时出现此错误:

  

错误:循环依赖:       在错误()       at getService(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2884:17)       在调用(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2909:13)       在Object.instantiate(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:2943:23)       在http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:5368:28       在http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:4937:17       at forEach(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:155:20)       at nodeLinkFn(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:4922:11)       在compositeLinkFn(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:4550:15)       在compositeLinkFn(http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:4553:13

我在这里缺少什么?我之前使用过相同的语法,它工作得很好

1 个答案:

答案 0 :(得分:14)

原来我已经将控制器包括两次了。在路由中以及HTML模板中。我现在已经从HTML模板中删除了它,一切正常!