相关疑难解决方法(0)

为什么我的$ scope属性会在我的AngularJS应用程序中继续重置?

我有以下AngularJS应用程序,包括template(index.html),app definition(app.js),控制器定义(controllers.js)和托管页面(host.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>
Run Code Online (Sandbox Code Playgroud)

app.js

angular.module('MyApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
     when('/', {templateUrl:'/index.html', controller: MyController}).
     otherwise({redirectTo: '/'});
}]);
Run Code Online (Sandbox Code Playgroud)

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);
}];
Run Code Online (Sandbox Code Playgroud)

index.html和host.jsp未显示简洁及其无关紧要.

控制器从Ajax请求中获取一些数据,将其中的一些存储在其中$scope以避免再次请求它并将其显示给用户并等待输入.当用户在视图中选择一些数据时,我更新URL查询部分以反映选择更改.但我想通过检查数据是否可用来避免后续的Ajax请求$scope.

我面临的问题$scope.myAttribute是始终未定义.它会根据每个请求进行重置.我想我在滥用AngularJS.任何线索?

javascript angularjs

6
推荐指数
1
解决办法
5219
查看次数

标签 统计

angularjs ×1

javascript ×1