私は現在、AngularJSプロジェクトを少し進めています。
私のサービスでこのコードを取得しました。
testApp.service('testService',['databaseService', 'loggedService', '$http', function(databaseService, loggedService, $http){
this.getDetails= function(name){
$http({
method : "GET",
url : "url"
}).then(function mySucces(response) {
return response.data;
}, function myError(response) {
$scope.myWelcome = response.statusText;
});
};
}]);
私は、コントローラの私のサービスからメソッドを呼び出しています:
testApp.controller('mainController', ['$scope', '$http', 'testService', function($scope, $http, testService){
$scope.details;
$scope.loadDetails= function(val){
$scope.details= testService.getDetails(val);
console.log($scope.details);
console.log(val);
};
}]);
ユーザーが検索フィールドに何かを入力しているとき、$ scope.loadDetailsが私の見解で呼び出されています。したがって、ドロップダウンを自動更新する必要があります。
サービスのresponse.dataは配列を返します。 しかし、コントローラの$ scope.detailsは未定義のままです。誰でもサービスからコントローラにresponse.dataを返す方法を知っていますか?
$ httpの外でresponse.dataを返そうとしましたが、コントローラで未定義のままです。
あなたがコントローラでサービスを注射しています? –
サービスとコントローラの依存関係リストを追加できますか?何かを注入しなかったと思われます\ –