2016-07-09 20 views
0

$http.get私のDB(mongoDB)からデータを取得しようとしているときに問題があります。

平均スタックを使用しています。また

angular.module('wildroseApp') 
.controller('WildroseComponent', function($scope, $http){ 
    $http.get('/api/wildrose') 
     .success(function (data) { 
     $scope.wildrose = data; 
     console.log($scope.wildrose); 
    }) 
     .error(function (err) { 
     alert('Error!'); 
    }); 
}) 
.component('wildrose', { 
    templateUrl: 'app/wildrose/wildrose.html', 
    controller: WildroseComponent, 
    controllerAs: Wildrose 
}); 

、私は1つの以上の質問があります。

私はコンポーネントでcontrollerAsを使用しています、私のブラウザは

Uncaught ReferenceError: Wildrose is not defined

言うがしかし、それを削除した場合、それはOKだろう。

ただ、コンストラクタで少しのコードを追加します:私は次のようにそれを解決しようとしている

class WildroseComponent { 
    constructor($http) { 
    this.$http = $http; 
    this.wildrose = []; 
    } 
    $onInit(){ 
     this.$http.get('/api/wildrose') 
      .then(response => { 
       this.wildrose = response.data; 
       console.log(this.wildrose); 
      });; 
    } 
} 
+0

使用しているAngularJのバージョンはどれですか? –

+0

バージョンは1.5.0です –

+0

あなたの質問/問題は何ですか? 「うまくいかない」とはどういう意味ですか? – dfsq

答えて

0

ControllerAsは、文字列ではなくオブジェクトを必要とします。

controllerAs: "wildrose" 

はあなたを持っていますあなたのデータベースで読み取ったjson文字列を返す/api/wildroseのnode/expressでサーバ上にエンドポイントを設定しますか?

バックエンドとエラーからより多くのコードを提供してください。そうでなければ、診断するのは難しいです。

+0

YEP私はこのコントローラ からコード// Wildroses エクスポート機能指数(REQ、RES){ リターンWildrose.find()のリストを取得しているヨーマン発生 とエンドポイントを作った。EXEC() .then(respondWithResult( res)) .catch(handleError(res)); } これはindex.jsです router.get( '/'、controller.index); $ http.getを使用しているときに何も表示されません。 –

+0

chromeを使用している場合は、ネットワークツールパネルでdevツールで何を言っていますか?要求は正しく送信されましたか?あなたのバックエンドからの回答は何ですか? – Samuel

+0

私はMEANについてはわかりませんが、mongodbのfindメソッドでは引数としてオブジェクトが必要です。コレクションのすべてのエントリを検索する場合は、空のオブジェクト.find({})を使用します。 – Samuel

関連する問題