16

に依存するコントローラーがありTransactionServiceます。その方法の一つが

$scope.thisMonthTransactions = function () {
    $scope.resetTransactions();
    var today = new Date();
    $scope.month = (today.getMonth() + 1).toString();
    $scope.year = today.getFullYear().toString();
    $scope.transactions = Transaction.getForMonthAndYear();
};

TransactionServiceように見えます

angular.module('transactionServices', ['ngResource']).factory('Transaction', function ($resource, $rootScope) {
    return $resource('/users/:userId/transactions/:transactionId',
        // todo: default user for now, change it
        {userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810', transactionId: '@uuid'},
        {
            getRecent: {method: 'GET', params: {recent: true}, isArray: true},
            getForMonthAndYear: {method: 'GET', params: {month: 5, year: 2013}, isArray: true}
        });
});

ご覧のとおり、このメソッドgetForMonthAndYearは と の 2 つのパラメータに依存してmonthおりyear、現在は としてハードコードされていparams: {month: 5, year: 2013}ます。このデータをコントローラーから渡すにはどうすればよいですか?

に注入しようとrootScopeしましTransactionServiceたが、それは役に立ちませんでした(つまり、おそらく使用方法がわからないということです)。

また、Angular ngResource のドキュメントでは、これを実行する方法は推奨されていません。

誰かがここを案内してもらえますか?

更新
私のコントローラーは次のようになります

function TransactionsManagerController($scope, Transaction) {

    $scope.thisMonthTransactions = function () {
        $scope.resetTransactions();
        var today = new Date();
        $scope.month = (today.getMonth() + 1).toString();
        $scope.year = today.getFullYear().toString();

        var t = new Transaction();
        $scope.transactions = t.getForMonthAndYear({month: $scope.month});
    };
}

サービス方法を次のように変更します

getForMonthAndYear: {method: 'GET', params: {month: @month, year: 2013}, isArray: true}

私は見てconsole.log、それは言う

Uncaught SyntaxError: Unexpected token ILLEGAL transaction.js:11
Uncaught Error: No module: transactionServices 
4

3 に答える 3

5

私は同じ問題を抱えていました。サービスからパラメーター化された関数を返すことになったので、サービスはそのようになります

factory('MyService', function($resource) {
    return {
        id: function(param) { return $resource('/api/'+param+'/:id', {id: '@id'}); },
        list: function(param) { return $resource('/api/'+param); },
        meta: function(param) { return $resource('/api/meta/'+param); }
    }
})

そして、コントローラーから次のようにサービスを呼び出します。

$scope.meta = MyService.meta('url_param').query();

これが最もクリーンな angular.js ソリューションかどうかはわかりませんが、機能します!

于 2013-08-16T14:05:13.250 に答える
5

リソース コンストラクターでパラメーターを定義する必要があるのは、何も指定されていないときに呼び出しに既定値が必要な場合のみです。メソッドに渡されたすべてのパラメータは、デフォルト値が定義されているかどうかに関係なく、クエリ パラメータとして追加されます。「@」は、params 値が JSON 応答で返される値に置き換えられることを意味するため、@uuid は意味がありますが、@month は意味がありません。

個人的には、次のようにリソースを作成します。

$resource('/users/:userId/transactions/:transactionId',
    // todo: default user for now, change it
    {userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810', transactionId: '@uuid'},
    {
        getRecent: {method: 'GET', params: {recent: true}, isArray: true},
        getForMonthAndYear: {method: 'GET', isArray: true}
    });

次に、必要に応じてクエリ変数を追加します (特別なメソッド名を作成することは問題ありませんが、必須ではないため、以下に両方の方法を示します)。

var t = new Transaction();
$scope.recentTransactions = t.$get({recent:true}) //results in /users/bd675d42-aa9b-11e2-9d27-b88d1205c810/transactions/?recent=true
$scope.recentTransactions = t.$getRecent(); //same thing as above
$scope.transactions = t.$get({month: $scope.month, year: $scope.year}); //results in /users/bd675d42-aa9b-11e2-9d27-b88d1205c810/transactions/?month=5&year=2013
$scope.transactions = t.$getForMonthAndYear({month: $scope.month, year: $scope.year}); //same as above... since no defaults in constructor, always pass in the params needed
于 2013-06-05T13:23:45.440 に答える
3

これを行う 1 つの方法は、サービスをコントローラーに挿入することです。

angular.controller('controllerName', 
 ['$scope', 'Transaction',
   function($scope, transactionService) {
      ...
   }
]);

その後、transactionService パラメーターを介してサービスのインスタンスにアクセスできます。

編集

このフィドルをチェックしてくださいこれがあなたがやろうとしていることに十分近いかどうか教えてください

于 2013-06-05T04:03:55.757 に答える