2016-05-16 79 views
2

我被卡住了,任何人都可以帮助我。这里是代码。我正在写grabData服务从url中获取数据。然后在控制器firstcontroller我根据搜索箱过滤数据:这是代码:从一个控制器访问数据到另一个

.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 
}) 

任何一个可以请帮助解决这个PROBL EM。

+1

缓存服务中的数据并引用它(如果它已经存在)。如果需要,强制刷新。 [类似这样](http://stackoverflow.com/questions/19648345/angularjs-factory-only-called-once)。然后根据需要将服务注入到控制器中。 – mariocatch

回答

3

写你的服务就是这样,

.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 
}) 

希望它能帮助。如有疑问,请在评论中提问。

+0

谢谢你的回复..我正在做过滤,在** firstcontroller **。我想在我的remainging控制器中使用来自** firstcontroller **的过滤数据。 – naik3

+0

只能在grabData服务中写入过滤代码吗?如果是这种情况,我如何从HTML过滤输入并将其传递给** grabData **服务进行过滤。 – naik3

+0

在你的'grabData'服务中,编写2个函数,'setFilteredData(data)'和'getFilteredData()'。从第一个控制器调用'setFilteredData',从另一个调用'getFilteredData'。这将完成这项工作。由于一切都发生在Angular世界中,任何一个部分的改变都会自动触发所有其他部分的改变。 –

-1

你需要注入工厂:

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

这同样适用于你的其他控制器。

+0

如果showData()是一个昂贵的服务器调用呢?你打算为每个控制器多次调用它吗? – user1620220

+0

那么如果是这种情况,他可能会更谨慎地使用ui-router,将呼叫置于父母状态的解决方案中,然后让孩子拥有控制器。然后,每个人都可以访问父级的解决方案,该解决方案将单个调用保存到showData()。 – rrd

+0

如果所有控制器手段都使用相同的代码,我是否必须为相应的HTML,div标签编写HTML代码? – naik3

相关问题