【发布时间】: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