2016-05-26 12 views
0

私はui-routerを使用しており、解決策を使用することはできません。 は、ここに私のapp.js状態はそれがうまく私のサービスを呼び出して、私は自分のアプリケーションでいくつかの場所でこのサービスを使用していますので、私はそのサービスが返すデータがうまく知っ

.state('addInvestment', { 
    url: '/addUpdateInvestment', 
    templateUrl: 'js/angular/modules/investment/views/AddUpdateInvestment.html', 
    resolve: { 
     InvestmentTypes: ["investmentService", function (investmentService) { 
      console.log("Resolving dependency..."); 
      return investmentService.getInvestmentTypes(); 
     }] 
    } 
}) 

に見えるです。

Chrome Error displayed

答えて

0

あなたはDEVコンソールを開く場合は、コンソールにそれをログに記録する必要があり、あなたのplunkerをフォークしてきました。私のサービスでは、応答を得た後、handleResponseというメソッドが呼び出されていました。しかし、そのメソッドは、戻り値の型ではありませんでした。私が解決型Vsから呼び出すと、コントローラー内の通常のサービスとして呼び出すと、どのように変化しますか分かりません。しかし、それは何らかの理由で解決を破っていました。 はここ

getInvestmentTypes:function() 
     { 
      return $http.get(this.webServiceUrl+'getAllInstitutionsForDisplay').then(function(response){return response.data}, handleError); 
     } 

私は同じようなことを行う必要があります。..下図のように私の古いサービス...新しいコードで

angular.module('InvestmentModule').factory('investmentService', ['$rootScope', '$http', '$q', function($rootScope, $http, $q){ 
    return{ 

     getInvestmentTypes:function() 
     { 
      return $http.get(this.webServiceUrl+'getAllInstitutionsForDisplay').then(handleResponse, handleError); 
     } 
} 
function handleSuccess(response) 
    { 
     return response.data; 
    } 

    function handleError(response) { 
     // The API response from the server should be returned in a 
     // nomralized format. However, if the request was not handled by the 
     // server (or what not handles properly - ex. server error), then we 
     // may have to normalize it on our end, as best we can. 
     if (
      ! angular.isObject(response.data) || 
      ! response.data.message 
     ) { 
      return($q.reject("An unknown error occurred.")); 
     } 
     // Otherwise, use expected error message. 
     return($q.reject(response.data.message)); 
    } 
}]); 

が、私はこの方法自体にhandleSucessにコードを追加していますハンドルエラーのためにも私は信じています..

しかし、私は今は美しく動作します。

のみ、このアプローチの問題は、それがDRYアプローチを壊しているということです。..それを改善するために、任意の提案が理解されるであろう。..

0

あなたはコンマを削除する必要があります:私はクロームでは以下を参照してください、私はページをロードすると

angular.module('InvestmentModule') 
    .controller('AddUpdateInvestment', 
    ['$scope', 'toaster', '$state', '$stateParams', '$wizard', 'investmentService', 'InvestmentTypes', 
    function ($scope, toaster, $state, $stateParams, $wizard, investmentService, InvestmentTypes) { 
     $scope.modalData = {}; 
     $scope.modalData.investmentTypes = InvestmentTypes.items; 
    }]); 

は、ここに私のコントローラのように見えるです。 解決が完了した後、コントローラをロードする必要があります。ほとんどの場合、このような状態でそのコントローラを使用しようとすることができます。

.state('addInvestment', { 
     url:'/addUpdateInvestment', 
     controller: 'AddUpdateInvestment as aui', 
     templateUrl:'js/angular/modules/investment/views/AddUpdateInvestment.html', 
     resolve: {InvestmentTypes: ['investmentService', function(investmentService) { 
      console.log("Resolving dependency..."); 
      return investmentService.getInvestmentTypes(); 
     }]}, // this will crash if you dont specify other parameter to the state. 
    }) 

EDIT:

チェックこの問題を修正する場合

InvestmentTypes: ['investmentService', // use single quotes 

EDIT 2:

resolve: { 
    InvestmentTypes: function(investmentService) { 
      console.log("Resolving dependency..."); 
      return investmentService.getInvestmentTypes(); 
     } 
} 

解決における注射は、このようなものですようです

お願いします。 wikiを見てくださいhere

+0

私は提案された解決策を試してみましたが、それはまだクロームコンソールで同じエラーがスローされます。 –

+0

一括見積もりで問題が解決されているかどうかを確認しました。 –

+0

Mateo、残念ながら運がありません。同じエラー。 –

0

私は問題を見つけたと思います。

あなたはそれが動作するはず

resolve: { 
    InvestmentTypes: ['investmentService', function(investmentService){ 
     return investmentService.getInvestmentTypes(); 
    }] 
} 

ようにそれを変更する場合は、引用符

resolve: { 
    'InvestmentTypes': ['investmentService', function(investmentService){ 
     return investmentService.getInvestmentTypes(); 
    }] 
} 

でInvestmentTypesを持っているあなたの決意で

。私は最後に、 は闘争のそんなにした後、私はそれが働いて作っ

http://plnkr.co/edit/KOxgOqsGJcTD1URuHEOn?p=preview

+0

ジョージ、私はそれを試して、私はまだ同じ問題があります。過去にも引用符を削除しようとしましたが、うまくいきませんでした。 –

+0

@AndyJohnson Plnkrをチェックすると、HTMLに "ng-app"というタグがあります。 – George

+0

はい。私のアプリケーションは100%角であり、これまでのところ、他のものはうまく動作しています。私は実際にPlnkrにng-appを追加することを忘れていた –

関連する問題