【问题标题】:Accessing data from one controller to another从一个控制器访问数据到另一个
【发布时间】:2016-05-16 15:39:41
【问题描述】:

我卡住了,谁能帮帮我。这是代码。我正在编写 grabData 服务以从 url 获取数据。然后在控制器 firstcontroller 我根据 search 框过滤数据:这是代码:

.factory("grabData",['$http',function($http){
    return{
        showData:function(){
            return $http.get("/http://localhost:5555/sampleData.json");
        }
    }
}])
.controller('firstController',function($scope, $filter,grabData) {
         grabData.showData().success(function(data){
         $scope.items = data;
         $scope.items1 = $scope.items;

         $scope.$watch('search', function(val){
              $scope.items = $filter('filter')($scope.items1, val);
         });
}

HTML 代码为:<div ng-controller="firstController"> <input type="text" ng-model="search"> </div>

谁能帮我在下一个控制器中显示 $scope.items

.controller('secondcontroller',function($scope){
   // Here I want to use $scope.items , from first controller
})
.controller('thirdcontroller',function($scope){
   // Here I want to use $scope.items , from first controller
})
.controller('fourthcontroller',function($scope){
   // Here I want to use $scope.items , from first controller
})

谁能帮忙解决这个问题。

【问题讨论】:

  • 缓存服务中的数据,如果已经存在则引用。如果需要,请强制刷新。 Something like this。然后根据需要将服务注入您的控制器。

标签: angularjs


【解决方案1】:

这样写你的服务,

.service("grabData",['$http', '$q', function($http, $q){

    var sampleData = null;
    var filteredData = null;

    this.showData = function () {
        var deferred = $q.defer();

        if(sampleData!=null){  
           //if data has already been fetched from server before, serve it
            deferred.resolve(sampleData)
        } 
        else {   
            //else, fetch the data from server, and store it for future use
            $http.get("/http://localhost:5555/sampleData.json").then(function(res){
              sampleData = res.data;
              deferred.resolve(sampleData);
            })
        }
        return deferred.promise;
     };

     //call this from controller1, inside your watch
     this.setFilteredData = function(data){
        filteredData = data;
     };

     //call this from other 2 controllers
     this.getFilteredData = function(){
       return filteredData;
     };


    }])

然后像这样修改你的控制器,

.controller('secondcontroller',function($scope, grabData){
   // do whatever you want to do with grabData
   //use that "grabData.showData().success" pattern as it is still a promise
})
.controller('thirdcontroller',function($scope, grabData){
   // do whatever you want to do with grabData
   // call grabData.getFilteredData() from here
})
.controller('fourthcontroller',function($scope, grabData){
   // do whatever you want to do with grabData
   // call grabData.getFilteredData() from here
})

希望对您有所帮助。如有疑问,请在 cmets 中询问。

【讨论】:

  • 谢谢您的回复。我正在做过滤,在 firstcontroller 中。我想在剩余的控制器中使用来自 firstcontroller 的过滤数据。
  • 我可以只在grabData服务中编写过滤代码吗?如果是这种情况,我如何从 HTML 中获取过滤输入并将其传递给 grabData 服务进行过滤。
  • 在您的 grabData 服务中,编写 2 个函数,setFilteredData(data)getFilteredData()。从第一个控制器调用setFilteredData,从另一个控制器调用getFilteredData。这将完成这项工作。由于一切都发生在 Angular 世界中,任何一个部分的变化都会自动触发所有其他部分的变化。
  • 不客气 :)
  • 嗨..我试过上面提到的代码。它给出错误“grabData.setFilterData 不是函数”你能帮帮我吗?
【解决方案2】:

你需要注入工厂:

.controller('firstController', ['grabData', function($scope, $filter,grabData) {
  // Your code here
}]);

其他控制器也是如此。

【讨论】:

  • 如果 showData() 是一个昂贵的服务器调用怎么办?您要为每个控制器多次调用它吗?
  • 好吧,如果是这样的话,他可能更谨慎地使用 ui-router,将调用置于父状态的解析中,然后让子状态拥有控制器。然后每个人都可以访问父级的解析,它保存对 showData() 的单个调用。
  • 如果所有控制器都使用相同的代码,我是否必须为相应的 HTML、div 标签编写 HTML 代码..????
  • @naik3 这取决于你最终想要做什么。例如,为什么要有单独的控制器?如果它们都在同一页面上,那么您实际上只需要一个控制器。如果它们分布在多个页面和多个 $states/views(例如当您使用 ui-router 时),那么您可能想要执行我在上面的评论中建议的操作,并在解析中使用一个单一的服务调用。我会在我不工作的时候把所有这些都放在一个编辑中,让它更清楚一点。
猜你喜欢
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 2016-05-30
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多