我开始使用 AngularJS 进行开发。我很困惑这是否是在我的部分视图之间传递数据的正确设计。
现在我有一个加载器页面,我在其中做一些请求。
function PeopleController($scope,$http,$location){
$http.get('/location/-79.18925/43.77596').
success(function(data){
$scope.savePeopleResponse(data);
$location.url('/test');
});
}
然后在为 /test 加载的视图中
我只是打电话
<div ng-controller="resultController">
<div class="blueitem">{{getResultForPeople()|json}}</div>
</div>
[结果控制器]
function resultController($scope){
$scope.getResultForPeople = function(){
return $scope.getPeopleResponse();
}
}
并且 savePeopleResponse 和 getResultForPeople 被“缓存”在 rootScope 中
app.run(function($rootScope) {
var peopleResponse = {};
$rootScope.savePeopleResponse = function(data) {
peopleResponse = data;
console.log(data);
}
$rootScope.getPeopleResponse = function(){
return peopleResponse;
}
});
现在您可以看到,如果这个应用程序变得越来越大,这将变得非常混乱。处理数据以使其在控制器之间持久存在的最佳方法是什么?