7

注:この質問は、AngularJSメーリングリストにも投稿しました:https ://groups.google.com/forum/#!topic / angle / UC8_pZsdn2U

こんにちは、みんな、

私は最初のAngularJSアプリを作成していますが、そもそもJavascriptにあまり詳しくないので、ガイダンスをいただければ幸いです:)

私のアプリには、ClientControllerとCountryControllerの2つのコントローラーがあります。CountryControllerで、$resourceオブジェクトを使用するCountryServiceから国のリストを取得しています。これは問題なく機能しますが、国のリストをClientControllerと共有できるようにしたいと思います。調査の結果、CountryServiceを使用してデータを保存し、そのサービスを両方のコントローラーに挿入する必要があることを読みました。

これは私が以前持っていたコードでした:

CountryService:

services.factory('CountryService', function($resource) {
    return $resource('http://localhost:port/restwrapper/client.json', {port: ':8080'});
});

CountryController:

//Get list of countries         
//inherently async query using deferred promise
$scope.countries = CountryService.query(function(result){       
        //preselected first entry as default
    $scope.selected.country = $scope.countries[0];  
});

そして、私の変更後、それらは次のようになります。

CountryService:

services.factory('CountryService', function($resource) {
    var countryService = {};

    var data;
    var resource = $resource('http://localhost:port/restwrapper/country.json', {port: ':8080'});

    var countries = function() {
        data = resource.query();
        return data;
    }

    return {
        getCountries: function() {
            if(data) {
                console.log("returning cached data");
                return data;
            } else {
                console.log("getting countries from server");
                return countries(); 
            }

        }
    };
  });

CountryController:

$scope.countries = CountryService.getCountries(function(result){
        console.log("i need a callback function here...");
});

問題は、以前は$ resource.query()のコールバック関数を使用してデフォルトの選択を事前に選択できたのですが、countryService内にquery()呼び出しを移動したため、何が失われたように見えました。 。

この問題を解決するための最善の方法は何ですか?

助けてくれてありがとう、ショーン

4

4 に答える 4

4

OK..soは、resource.query()呼び出しまでコールバック関数を渡すことで問題を解決したようです。これがこれを行うための最良の方法であるかどうかはまだわかりません。

参考までに、これは私がしたことです:

CountryController:

$scope.countries = CountryService.getCountries(function(){
    //preselected default
    $scope.selected.country = $scope.countries[0];  
});

CountryService:

//Country Service. Will contain all relevant rest methods here
services.factory('CountryService', function($resource) {
    var countryService = {};

    var data;
    var resource = $resource('http://localhost:port/restwrapper/country.json', {port: ':8080'});

    var countries = function(callback) {
        data = resource.query(callback);
        return data;
    }


    return {
        getCountries: function(callback) {
            if(data) {
                console.log("returning cached data");
                return data;
            } else {
                console.log("getting countries from server");
                return countries(callback); 
            }

        }
    };
  });
于 2012-10-08T21:34:43.157 に答える
1

$qサービスを確認する必要があります。次のことができます。

promise.then(callback);

$then1.1.3から、たとえば、を使用してpromiseにアクセスできます。resource.query().$then(callback);

于 2012-10-08T14:50:48.107 に答える
0

この新しいシナリオでは、$scope。$watchを使用できます。使い方は以下の通りです。

$scope.countries = CountryService.getCountries();

$scope.$watch('countries',function(newValue){
//need to check if newValue has a value because 
//the first time watch fires it will be undefined.
    if(newValue && newValue.length > 0 ){
        $scope.selected.country = $scope.countries[0];
    }
});

お役に立てれば。

于 2012-10-08T10:39:52.000 に答える
0

AngularJSには、クエリをキャッシュする独自の方法があり、これを使用する必要があります。コールバックに関しては、とにかくpromiseが返されるので、変数にクエリを割り当てても意味がありません。あなたは単に常に約束を期待し、成功の中で、または最後にあなたのコールバックを渡すべきです。

于 2014-06-28T20:43:02.650 に答える