2016-09-15 6 views
3

こんにちは私はbillerModelという名前のモデルと、billersの変数を持つ解決策を持つルートを作成しました。今私は取得し、私のコントローラ内でこの変数を割り当てたいが、私はこのbillerData不明プロバイダエラーを取得します。以下は、ルートの私のコードです:変数を取得してコントローラに渡す

以下
app.config(["$routeProvider", "$httpProvider",'$locationProvider',function($routeProvider, $httpProvider, $locationProvider) { 

$routeProvider 
    .when("/billers", { 
     templateUrl: 'admin/billers/index.html', 
     controller: 'billerController', 
     resolve: { 
      billerData: function(billerModel) { 
       return billerModel.getData(); 
      } 
     } 
    }); 

は、モデル以下

app.factory('billerModel', ['$http', '$cookies', function($http, $cookies) { 
    return { 
     getData: function() { 
     return $http({ 
      method: 'GET', 
      url: 'billers' 
     }); 
     } 
    } 
}]); 

ための私のコードである私もしようとした私にエラーを与える私のコントローラのコード

app.controller('billerController', ['$scope', 'billerData', 
    function($scope, billerData){ 
     $scope.billers = billerData; 

}]); 

です私のビューからng-controllerを削除しますが、これを行うと未知の変数のエラーが発生します

<div style="text-align: left;" ng-controller="billerController"> 
    {{ billers }} 
</div> 
以下は

enter image description here

あなたが変更してみてくださいでしjsfiddleですが、私はそれを使用する方法の慣れていないんだが、基本的な構造は、ここで https://jsfiddle.net/bd06cctd/1/

+0

あなたは工場 'billerModel'を作成します。しかし、コントローラ 'billerData'に注入してみてください。 'billerModel'にインジェクションを変更してください。 –

答えて

2

.whenのブロックデータは、.whenブロックで定義されたコントローラにのみ注入可能です。 ng-controllerディレクティブで注入された子コントローラは、解決データを注入できません。あなたは.whenブロックテンプレートでng-controllerディレクティブで.whenブロックbillerControllerを注入した場合

また、コントローラは二回をインスタンス化されます。

$routeProviderは、$resolveプロパティのビュースコープで利用可能なデータを解決します。 ng-controllerディレクティブでインスタンス化された子コントローラはそれを使用できます。 ng-controllerディレクティブでインスタンス化

app.controller('childController', ['$scope', 
    function($scope){ 
     $scope.billers = $scope.$resolve.billerData; 
}]); 

コントローラは、ビューのスコープからプロトタイプ継承によって$resolveプロパティがあります。ドキュメントから

  • resolve - {Object.<string, Function>=} - An optional map of dependencies which should be injected into the controller. If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. If all the promises are resolved successfully, the values of the resolved promises are injected and $routeChangeSuccess event is fired. If any of the promises are rejected the $routeChangeError event is fired. For easier access to the resolved dependencies from the template, the resolve map will be available on the scope of the route, under $resolve (by default) or a custom name specified by the resolveAs property.

- AngularJS $routeProvider API Reference

+0

あなたはそれをはっきりと説明する人生保護者です。大変ありがとうございます –

-1

が含まれています

app.factory('billerModel', ['$http', '$cookies', function($http, $cookies) { 
return { 
    getData: function() { 
    return $http({ 
     method: 'GET', 
     url: 'billers' 
    }); 
    } 
} 
}]); 

app.factory('billerModel', ['$http', '$cookies', function($http, $cookies) { 
    return { 
     getData: $http({ 
      method: 'GET', 
      url: 'billers' 
     }); 
    } 
}]); 

あなたはanoを返していますニーモニック機能ではなく、プロミス

+0

同じ問題で運がない。 –

+0

JSFiddleを作成できますか? – nikjohn

+0

ここにフィドルがありますhttps://jsfiddle.net/madzmar25/bd06cctd/14/ –

関連する問題