Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 如何解决角度关系中的循环依赖关系_Angularjs_Circular Dependency_Angular Services - Fatal编程技术网

Angularjs 如何解决角度关系中的循环依赖关系

Angularjs 如何解决角度关系中的循环依赖关系,angularjs,circular-dependency,angular-services,Angularjs,Circular Dependency,Angular Services,我有一个新闻服务和个人服务,还有一个个人和新闻项目的模型。以下是不包括服务的简化表示 //angular service to expose Person var Person = function(data) { angular.copy(data, this); //some other initialisation } Person.prototype.getNews = function() { //fetch news .then(function(ne

我有一个
新闻服务
个人服务
,还有一个
个人
新闻项目
的模型。以下是不包括服务的简化表示

//angular service to expose Person
var Person = function(data) {
    angular.copy(data, this);
    //some other initialisation
}
Person.prototype.getNews = function() {
    //fetch news
    .then(function(newsItems) {
        return newsItems.map(function(newsItemData){
            newsItemData.author = new Person(newsItemData.author);
            return new NewsItem(newsItemData);
        });
    });
}

//Angular service to expose NewsItem
var NewsItem = function(data) {
    angular.copy(data, this);
    //some other initialisation
}
NewsItem.prototype.getComments = function() {
    //fetch comments
    .then(function(commentsData){
        return comments.map(function(commentData) {
            commentData.author = new Person(commentData.author);
            return new Comment(commentData);
        });
    });
}

//Angular services that reference these objects
NewsService
PersonService

NewsItem和Person相互引用,因为从它们各自的API获取的数据包括应该由另一个对象实例化的数据。有许多对相互引用的服务和模型,我有一个循环依赖的问题,这迫使我在别处实例化对象,这是我想要避免的。我想不出一个优雅的解决方案,真的需要你的帮助

使用3个对象:一个用于
新闻项目
,一个用于
人员
,一个用于关联这两个对象(称之为
项目
或任何你想要的)。使用3个对象:一个用于
新闻项目
,一个用于
人员
,一个用于关联这两个对象(称之为
项目
或任何你想要的)。