2016-09-29 10 views
6

私は最初の角度成分をngRouteでまとめようとしていますが、今までは解決するデータを取得できません。 設定:角度コンポーネントバインディング定義されていません

.when('/myfirstcomponent', { 
    template: '<myfirstcomponent claimKeys="$resolve.claimKeys"></myfirstcomponent>', 
    resolve: { 
     claimKeys: ['$http', function($http) { 
      $http.get('server/claimkeys.json').then((response) => { 
       var claimKeys = response.data.DATASET.TABLE; 
       return claimKeys; 
      }); 
     }] 
    } 
}) 

コンポーネント:

.component('myfirstcomponent', { 
     bindings: { 
      'claimKeys': '@' 
     }, 
     templateUrl: 'components/component.html', 
     controller: [function() { 
      this.$onInit = function() { 
       var vm = this; 
       console.log(vm.claimKeys); 
      }; 


     }] 

コンポーネントのHTMLは、単にすべてだいくつかのランダムなテキストを持つp要素を持っています。

デバッグ、私はデータを取得していますそのときに私が見ることができますが、私はコンポーネントコントローラ上でそれにアクセスすることはできません...

編集:私は私の問題を修正しました下の受け入れ答えに感謝します。非同期呼び出しの問題とは関係ありませんでしたが、私のルートとコンポーネントをどのように定義したのかとは関係ありませんでした。修正のためのコードを見てください。再度、感謝します。

+0

私の最初の問題は私のテンプレートにあります 'claimKeys'はクレームキーでなければなりません。しかし、単純に文字列 '$ resolve.claimKeys'に解決されます...進歩はほとんどありませんが、それ以上は得られません。 – Mickers

+0

[非同期呼び出しから応答を返すにはどうすればよいですか?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –

+0

'return claimKeys'は非同期なので' $ resolve.claimKeys'に何も返しません。二重引用符を参照してください。 –

答えて

8

いくつかの問題:あなたは

  • を言ったようにディレクティブ内が請求-キー
  • そのあるべき結合 '<'(片道結合)または '='(双方向である必要がありclaimKeysバインディング)を使用しますが、ディレクティブに渡す '@'ではなく、引用符で囲まれた文字列
  • ディレクティブのコントローラvar vm = this;$ onInit機能はない(スコープが異なる)
  • resolve.claimkeys $ HTTPの約束を返す必要がありますだけではなく、呼び出し、その中 それ
  • そのテンプレートに注射などのルータのコントローラによって受信され、渡されるべきclaimKeys
  • http://plnkr.co/edit/Nam4D9zGpHvdWaTCYHSL?p=previewcontrollerAs: '$resolve'

plunkerルータ

app.component('myfirstcomponent', { 
    bindings: { 
    'claimKeys': '=' 
    }, 
    template: 'components/component.html', 
    controller: function() { 
    var vm = this; 
    this.$onInit = function() {    
     console.log(vm.claimKeys); 
    }; 
    } 
}); 

app.config(function ($stateProvider) { 
    $stateProvider.state('myfirstcomponent', { 
    url: '/myfirstcomponent', 
    template: '<myfirstcomponent claim-keys="$resolve.claimKeys"></myfirstcomponent>', 
    resolve: { 
     claimKeys: ['$http', function($http) { 
     return $http.get('claimkeys.json').then((response) => { 
      return response.data.DATASET.TABLE; 
     }); 
     }] 
    }, 
    controller: function (claimKeys) { 
     this.claimKeys = claimKeys; 
    }, 
    controllerAs: '$resolve' 
    }) 
}); 
で使用する必要があります私はここで。状態を使用し、ではなくをルーティングに使用しました。

+0

よく考えていただきありがとうございます。私は次の例から$ resolveを取った。私はそれを修正するときに私は別の名前を使用します。これらの変更を実装する機会があったら、これを私の答えと記しておきます。 – Mickers

関連する問題