2016-08-18 4 views
-2

このコードを見て、なぜこのエラーが発生し続けるのか理解してもらえますか?私はangularjs(特にサービス/ファクトリ関数の使用)が初めてです。関数とローカル変数が$スコープ変数として$サービスの代わりにコントローラに定義されていてもうまくいきましたが、この関数から返されるデータは他の複数のコントローラで必要とされるため、コードの冗長性を減らしたいと思っていました。Uncaught TypeError:未定義のメソッド 'push'を呼び出せません - Angularjs

app.service('SupportService', function() 
{ 
    var locations = []; //local variable to which other elements should be added 



//function to get data and update the local variable (this.locations) 

this.setLocations = function() 
    { 
     var temp_location = {}; 

     $.ajax 
     ({ 
      type: 'get', 
      url: 'myurl', 
      success: function(response) 
      { 

       for(var i = 0; i < response.length; i++) 
       { 
        temp_location = 
        { 
         id: response[i].id, 
         name: response[i].name 

        }; 
        locations.push(temp_location); //this is where the error is 
       } 
      }, 
      error: function(response) 
      { 

      } 

     }); 
    } 

サービスから可変場所にアクセスするための追加機能は

this.getLocations = function() 
    { 
     return locations; 
    } 

iは

app.controller('RSVPController', function($scope,SupportService,AuthService) 
{ 

    $scope.init = function() 
    { 
     SupportService.setLocations(); 
    } 

    $scope.locations = SupportService.getLocations(); 
}); 

を次のようにデータがバインドするために使用するコントローラとビューであるIこのコントローラのinit関数を呼び出して、次のようにselectに値を追加します。

<select ng-model="location" ng-options="item.name for item in locations track by item.id" required></select> 

ありがとうございます。

+0

「this」が変更されます。イベントハンドラでは、 'this'はイベントが発生した要素を参照します。 '$ .ajax'では、' this'がajaxオブジェクトになります。 – Tushar

+0

あなたの '$ .ajax'リクエストに' context:this 'を追加してください。 'app.service'コールバックで' this'にプロパティを追加するべきでしょうか? –

+0

@squintこれで問題は解決しません。そうすれば、 'this'はイベントが発生した要素を参照します。 – Tushar

答えて

0
app.service('SupportService', function() 
{ 
    this.locations = []; //local variable to which other elements should be added 
    var self = this; 

    enter code here 

//function to get data and update the local variable (this.locations) 

this.setLocations = function() 
    { 
     var temp_location = {}; 

     $.ajax 
     ({ 
      type: 'get', 
      url: 'myurl', 
      success: function(response) 
      { 

       for(var i = 0; i < response.length; i++) 
       { 
        temp_location = 
        { 
         id: response[i].id, 
         name: response[i].name 

        }; 
        self.locations.push(temp_location); //this is where the error is 
       } 
      }, 
      error: function(response) 
      { 

      } 

     }); 
    } 
+0

これは正しいものを指す必要があります。ここでは自己を使ってチェックアウトしています。 –

1

'this'を使うときは、異なるスコープで別のオブジェクトを指している可能性があるので注意しなければなりません。単にこれを削除するだけでクロージャに配置できます。サービスはシングルトンですが、それは覚えています。

+0

の代わりにAngulars '$ http'モジュールを使用してください。別のゲッター関数を追加するだけです –

+0

ありがとうございます。私の使用法 "this"をクリーンアップした後で動作しましたが、今ではこのデータを使用するselect要素の値を取得できないようです。オプションはビューに正しく表示されますが、値を選択してフォームを送信した後に、 'undefined'または '[object] [object]'を返します。ここでは、選択がどのように定義されているのですか? ' " –

+0

場所をバインドしたい、自己=これ。 ajax呼び出しの側でself.locationを使用するか、成功するとfunction(){...} bind(this) –

関連する問題