为什么我的$ scope属性在我的应用AngularJS一直在CH375复位? [英] Why my $scope attributes keep reseting in my AngularJS application?
问题描述
我有一个模板由以下AngularJS应用程序( index.html的
),应用程序定义( app.js
),控制器定义( controllers.js
),并托管网页( host.jsp
)。
I have the following AngularJS application composed of a template (index.html
), an app definition (app.js
), a controller definition (controllers.js
) and a hosting page (host.jsp
).
在code是如下:
search.jsp的
search.jsp
<div class="container-fluid" ng-app="facetedSearch">
<div class="row-fluid" ng-view>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src="/resources/js/page/search/app.js"></script>
<script src="/resources/js/page/search/controllers.js"></script>
app.js
app.js
angular.module('MyApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {templateUrl:'/index.html', controller: MyController}).
otherwise({redirectTo: '/'});
}]);
controller.js
controller.js
var MyController=['$scope','$http','$location',function ($scope, $http, $location) {
//do some fancy stuff
if($scope.myAttribute===undefined){
$scope.myAttribute=someDataFromHttpRequest;
}
$location.search(someParameters);
}];
的index.html和host.jsp未示出为简洁和它们无关。
index.html and host.jsp are not shown for brevity and their irrelevance.
控制器会从Ajax请求一些数据,存储它的一些在 $范围
,以避免再次请求它和显示它给用户,并等待输入。当用户选择视图中的一些数据,我更新URL查询部分,以反映选择更改。但我想通过检查以避免后续Ajax请求,如果数据是在 $范围可
。
The controller gets some data from Ajax request, stores some of it in the $scope
to avoid requesting it again and the shows it to the user and waits for input. When the user selects some data in the view, I update URL query part to reflect selection changes. But I want to avoid subsequent Ajax requests by checking if the data is available in the $scope
.
我面临的问题是, $ scope.myAttribute
永远是不确定的。它重置在每次请求。我觉得我滥用AngularJS。任何线索?
The problem I'm facing is that the $scope.myAttribute
is always undefined. It resets on every request. I think I'm misusing AngularJS. Any clue?
推荐答案
当你离开一个控制器,范围被摧毁。我会考虑让存储你想要的东西的服务。
When you leave a controller, the scope gets destroyed. I would look into making a service that stores the stuff you want.
angular.module('MyApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {templateUrl:'/index.html', controller: MyController}).
otherwise({redirectTo: '/'});
}])
.service("myService", function(){
this.myAttribute = null;
});
var MyController=['$scope','$http','$location', 'myService',function ($scope, $http, $location, myService) {
//do some fancy stuff
if(myService.myAttribute===null){
myService.myAttribute=someDataFromHttpRequest;
}
$location.search(someParameters);
}];
服务用于多个控制器之间共享日期/指令,所以我pretty肯定这是你想要的。
Services are used to share date between multiple controllers/directives so i'm pretty sure it's what you want.
下面是对他们的文档信息:<一href=\"http://docs.angularjs.org/guide/dev_guide.services.creating_services\">http://docs.angularjs.org/guide/dev_guide.services.creating_services
Here's the doc information on them: http://docs.angularjs.org/guide/dev_guide.services.creating_services
这篇关于为什么我的$ scope属性在我的应用AngularJS一直在CH375复位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!