6

index.htmlテンプレート ( )、アプリ定義 ( app.js)、コントローラー定義 ( controllers.js)、およびホスティング ページ ( )で構成される次の AngularJS アプリケーションがあります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>

app.js

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

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 は、簡潔さと関連性のために表示されていません。

コントローラーは Ajax リクエストから一部のデータを取得し、$scope再リクエストを避けるためにその一部を に保存し、それをユーザーに表示して入力を待ちます。ユーザーがビューでデータを選択すると、選択の変更を反映するように URL クエリ パーツを更新します。しかし、データが$scope.

私が直面している問題は、$scope.myAttributeが常に未定義であることです。リクエストごとにリセットされます。私はAngularJSを誤用していると思います。どんな手掛かり?

4

2 に答える 2

13

コントローラーを離れると、スコープが破棄されます。あなたが欲しいものを保存するサービスを作ることを検討します。

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);
}];

サービスは、複数のコントローラー/ディレクティブ間で日付を共有するために使用されるため、それがあなたが望むものであると確信しています。

それらに関するドキュメント情報は次のとおりです。http://docs.angularjs.org/guide/dev_guide.services.creating_services

于 2012-12-18T20:05:31.617 に答える
4

サービス (ま​​たは $rootScope) を使用して、保持する情報を保存する必要があります。サービスはシングルトンであり、それらをコントローラーに挿入できます。そこで設定したものはすべてアプリケーションに保持されます。

$scopes は、ルートを変更すると削除されるため、保持されません。

次に例を示します。

var myApp = angular.module('myApp',[]);
myApp.factory('SomeService', function() {
  return {
      myAttribute : '12345'
  };
});

var MyController=['$scope','$http','$location', 'myService', 'SomeService',function ($scope, $http, $location, myService, SomeService) {
   //do some fancy stuff
   SomeService.myAttribute; //this is how you access myAttribute
}];

また、サービス内に関数を作成して、AJAX を介して必要な値を取得するので (コントローラー内ではなく)、サービスは次のようになります。

myApp.factory('SomeService', function() {

    var SomeService = {};

    var myAttribute;

    SomeService.getMyAttribute = function () {

         if(!myAttribute) {

            //run ajax request then populate and return myAttribute

         }
         return myAttribute;
    };
    return SomeService;
});
于 2012-12-18T20:29:10.493 に答える