为什么我的$ scope属性在AngularJS应用程序中不断重置? [英] Why my $scope attributes keep resetting in my AngularJS application?

查看:73
本文介绍了为什么我的$ scope属性在AngularJS应用程序中不断重置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下由模板(index.html),应用程序定义(app.js),控制器定义(controllers.js)和托管页面(host.jsp)组成的AngularJS应用程序.

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

代码如下:

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

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请求中获取一些数据,并将其中一些存储在$scope中,以避免再次请求,并将其显示给用户并等待输入.当用户在视图中选择一些数据时,我更新URL查询部分以反映选择更改.但是我想通过检查$scope中的数据是否可用来避免后续的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);
}];

服务用于在多个控制器/指令之间共享日期,因此我很确定这就是您想要的.

Services are used to share date between multiple controllers/directives so i'm pretty sure it's what you want.

这是有关它们的文档信息: http://docs.angularjs.org/guide/dev_guide.services. creation_services

Here's the doc information on them: http://docs.angularjs.org/guide/dev_guide.services.creating_services

这篇关于为什么我的$ scope属性在AngularJS应用程序中不断重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆