2016-09-26 6 views
1

私はAngularJSの初心者ですが、自分のプロジェクト用に少しのモジュールを修正する必要があります。ここ は私のサービス工場である:

ここで私はそれを使用
services.factory('NewsService', function($resource) { 

    var result = $resource('http://localhost:8090/boiler/1'); 
    return result; 
}); 

function IndexController($scope, NewsService) { 

    var result = NewsService.get() 

    $scope.xxx= result.day; 
    $scope.www = result; 

私は$スコープ変数resultに送信できる理由を理解して{{www.day}}としてHTMLでそれを使用するが、私はできません「Tは、変数result.dayの$スコーププロパティに送信し、これは、サーバー

からの私のJSONレスポンスである {{xxx}}

としてそれを使用します

{ 
    "day" : "2016-10-06", 
    "amountOfEnergy" : 40, 
    "cost" : 120, 
    "content" : [ ], 
    "links" : [ { 
    "rel" : "self", 
    "href" : "http://localhost:8090/boiler/1" 
    }, { 
    "rel" : "bathroom", 
    "href" : "http://localhost:8090/boiler/1" 
    } ] 
} 

誰かが私にそれを説明できますか?

+0

あなたはどのようなエラーが出るのか、 '$ scope 'の様子を教えてください。 – Andurit

+0

私は両方の場合でチェックしたときに私は日を見ることができます.... –

+0

私はブラウザのコンソールでエラーをチェックしません – luafanti

答えて

1

$resource戻り約束ので、コントローラ

function IndexController($scope, NewsService) { 
    NewsService.get() 
     .$promise 
     .then(function(response){ 
      console.log(response); 
      var result = response; 
      $scope.xxx= result.day; 
      $scope.www = result; 
     }); 
} 
$promise.thenを使用
+0

これはうまくいきました。 – luafanti

1

私は、これは($resource DOCから)良くなると思う:

function IndexController($scope, NewsService) { 
    $scope.result = {}; 

    NewsService.get({}, function (data) { 
     $scope.result = data; 
    }); 
}; 

とテンプレートの{{result.day}}

+0

しかし、私は必要なので、$リソースオブジェクトから通常のJavaスクリプトオブジェクトを取得する方法JSコードで "day"のようなプロパティにアクセスできるようにします。 – luafanti

+0

NewsService.get - 非同期関数であり、結果で操作できるコールバックが必要です。 $ scope.result - 通常のオブジェクトであり、テンプレートに直接使用することもできます。コールバックでは、 'console.log($ scope.result.day)'のような何でもできます。 –

関連する問題