【问题标题】:Using data from one controller in another在另一个控制器中使用来自一个控制器的数据
【发布时间】:2017-11-18 22:43:16
【问题描述】:

我正在制作一个基于 MEAN 的网站。现在我试图将数据从一个控制器传递到下一个控制器,但我似乎没有完成它。选择选项时,我希望将此值置于下一页

这是带有选择框的页面:

<div class="plumber-by-city col-sm-12 home-text-col" ng-controller="DataCtrl">
<div class="title-1">Search by cityname</div>
   <select ng-model="selectedItem" ng-change="change()" ng-options="item as item.city for item in items"></select><span class="fa fa-caret-down"></span>
</div>

在更改时,我想使用 DataCtrl2 将 selectedItem 传递到下一页

这是我的 DataCtrl:

app.controller('DataCtrl', function($scope,$http,getData,$location,$routeParams){

   getData.getCity($scope);

   $scope.change = function() {
      $location.path('/plumber-in/' + $scope.selectedItem.city);
   };
});

以及从数据库中检索数据的服务:

app.factory('getData', function($http){
  return {
     getCity: function($scope){
       return $http.get("/getdata").then(function(response){
         $scope.items = response.data;
         $scope.selectedItem = response.data[0];
       });
     }
   };
});

现在我不知道在第二页的 DataCtrl2 中放什么,以及如何在这个页面中使用 DataCtrl 中的数据

【问题讨论】:

  • 是否可以创建一个有效的 JSFiddle?您可以使用服务在控制器之间保持(使用 setter / getter)活动城市,或使用 stateparams 将此数据传输到下一个状态。
  • @Larsmanson:让它变得简单,从工厂\服务返回承诺,并在控制器中进行评估。 :)
  • @anoop:我怎么能这样做。我不熟悉这个
  • @Larsmanson :查看question on SO 的答案,下面的答案sachila 也做了同样的事情。

标签: javascript html angularjs mean


【解决方案1】:

不要在工厂内使用$scope。只需将请求承诺返回给控制器并在那里执行您的逻辑。

app.factory('getData', function($http){
  return {
     getCity: function(){
       return $http.get("/getdata");
     }
   };
});

像这样修改控制器

app.controller('DataCtrl', function($scope,$http,getData,$location,$routeParams){

   getData.getCity().then(function(response) {
      $scope.items = response.data;
      $scope.selectedItem = response.data[0];
   });

   $scope.change = function() {
      $location.path('/plumber-in/' + $scope.selectedItem.city);
   };
});

【讨论】:

  • 您的答案是正确的,不知道谁投了反对票,进一步只需从getData.getCity($scope). 和工厂方法中删除$scope。 :)
  • 我赞成您的回答 - 因为有人投错了反对票...这是一个可能的解决方案...尽管我个人更喜欢上面的答案(使用service) . :)
  • @sachila:刚刚看到你回答。他打算在不同的控制器之间传递“从选项中选择的项目”。你的答案是如何解决这个问题的?
  • @mechanicals 他将它作为参数传递给`$location.path`。所以你不需要为此创建一个单独的工厂函数
  • 'When an option is selected I want to take this value to the next page'.我认为这个问题更多的是关于如何在控制器周围传递数据。因此,一种通用的方法是使用 setter getter 方法。
【解决方案2】:

您需要的是服务中的 Setter 和 Getter 方法。像这样的

app.controller('DataCtrl', 
    function($scope,$http,getData,$location,$routeParams){

   getData.getCity().then(function(response) {
      $scope.items = response.data;
      $scope.selectedItem = response.data[0];
   });

   $scope.change = function() {
      // sets the data to the service
      getData.setItem($scope.selectedItem);

      $location.path('/plumber-in/' + $scope.selectedItem.city);         
   };

});

app.factory('getData', function($http){
  return {
     getCity: function(){
       return $http.get("/getdata");
     },

     setItem: function(item) {
         this.item = item;
     },

     getItem: function() {
         return this.item;
     }
   };
});

app.controller('DataCtrl2', 
    function($scope, $http, getData,$location,$routeParams){
        // get the data from the service
        getData.getItem();
});

【讨论】:

  • 不知何故它没有转到其他页面但停留在同一页面上
  • @Larsmanson:你说的是数据还是页面浏览量?还有一个错字。已更新。
  • 浏览量没有变化
  • 您遇到了什么错误。您是否看到更新的更改。另外,只需将我添加到您现有工作代码中的代码部分添加,
  • 我已更新您的代码以不传递范围。看看吧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2015-08-18
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多