我被卡住了,任何人都可以帮助我。这里是代码。我正在写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。
缓存服务中的数据并引用它(如果它已经存在)。如果需要,强制刷新。 [类似这样](http://stackoverflow.com/questions/19648345/angularjs-factory-only-called-once)。然后根据需要将服务注入到控制器中。 – mariocatch