2017-02-17 146 views
0

我想将一个控制器的值传递给其他控制器。两个控制器都在同一个模块上。我尝试通过创建服务。问题是我能够将价值传递给第一控制器的服务,但无法在第二个控制器上检索该值。任何帮助,将不胜感激。在项目页面上给出。从控制器传递值到另一个页面上的另一个控制器 - AngularJS

控制器1(这是项页)

itpApp.controller("itpAppControllerKB", function($scope, sharedProperties, $location) { 
$scope.showArticles = function(myValue) { 

     console.log('myValue: ' + myValue); 

     sharedProperties.setListName(myValue); 

     window.location.href = "/portal/articles"; 
    }; 

}); 

控制器2(这是文章页)

itpApp.controller("itpAppControllerKBArticle", function($scope, sharedProperties) { 
$scope.article = sharedProperties.getListName(); 

console.log('selTopic: ' + $scope.article); 
} 

服务

itpApp.service('sharedProperties', function() { 
    var list_name = ''; 
    return { 

     getListName: function() { 
      return list_name; 
     }, 
     setListName: function(name) { 
      list_name = name; 
     } 
    }; 
}); 
+0

为什么你有服务的返回状态?工厂需要它,但不是服务。这是你的错误 – Gilsdav

+1

Does window.location.href =“/ portal/articles”;部队重新加载你的网页?万一你所有的服务数据丢失... – fubbe

+0

http://plnkr.co/edit/VEgt5v9VmeRPU4QIYArF?p=preview。如果我使用此链接的服务。它的工作,但我也获得了以前的所有价值。 – user3781360

回答

1

请勿在您的服务中使用return语句。这里是一个例子:

itpApp.service('sharedProperties', function() { 
    this.list_name = ''; 

    this.getListName = function() { 
     return this.list_name; 
    }; 

    this.setListName = function(name) { 
     this.list_name = name; 
    }; 
}); 
+0

@op使用这个和位置,你应该没问题。 – fubbe

相关问题