2017-05-31 13 views
1

私はAngularアプリで作業していますが、コントローラ内ではオブジェクトの配列を反復処理する必要があります。これは、コントローラと、このような状況に関与コードです:javascriptオブジェクトの配列を繰り返し処理する

TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined 
    at m.scope.login (loginController.js:44) 
    ... 

myapp.controller('LoginController', ['$scope', function(scope) { 
    // Users for test 
    this.users = [ 
     { 
      name: 'admin', 
      password: 'admin', 
      role: 'ADMIN' 
     }, 
     { 
      name: 'employee', 
      password: '12345', 
      role: 'EMPLOYEE' 
     } 
    ]; 
    console.dir(this.users); // Prints an array of objects correctly 

    // called when user submits 
    scope.login = function() { 
     console.log('login(). User: '); 
     console.dir(scope.user); // Prints the object with user's input (also correct) 

     var found = false; 

     for(let u of this.users) { 
      console.log('Comparing with:'); 
      console.dir(u); 

      if(u.name == scope.user.name && u.password == scope.user.password) { 
       console.log('Found!'); 
       found = true; 

       // do something else... 
      } 
     } 

     if(!found) { // show error message... } 
    } 
}]); 

問題は、私は、ログインフォームを送信するとき、私は、コンソールにエラーメッセージが表示されます(scope.login()に呼び出す)ということですloginController.js:44for(let u of this.users) {行に対応します。私はウェブ(W3 Schools、MDN、そしてこのサイトも検索)を検索しましたが、解決策は私のためには機能しませんでした。これはCannot read property 'length' of undefined

にエラーメッセージを変更し、私はそれは単純なものだということを感じているが、私は姿をcant't:

  • for(var u of this.users)
  • var u; for(u in this.users)
  • for(var i = 0; i < this.users.lenght; i++):私はすでに次の解決策を試してみましたそれは何か(私は非常にJavascriptで熟練していない、申し訳ありません)。誰もがこの問題で私を助けることができますか?

    ご回答いただきありがとうございます。

+0

を使用すると、 'this.users'未定義のですか? –

+2

なぜあなたは 'this'を使っていますか? 'scope'や普通の' var'や 'let'を使うべきでしょうか?なぜなら、関数に到達すると、 'this'は関数を指しているからです。 – Chifilly

+1

@Chifillyそれは' controller as'構文です。 '$ scope 'の方が好きです – mhodges

答えて

2

:これを試してみてください。あなたが書くことができますscope.login = function() {

var _this = this;

はその後_this.users.forEach(function(user) {

またはfor (var i = 0; i < _this.users.length; i++)

+1

私はあなたの答えを正しいと思います。私は 'var users = [...] '私はコントローラ全体でそれを使うことができ、完全に働きました。どうもありがとうございます –

1

scope.login = function() {}内の文脈の変更は、オブジェクトメソッドであるため、thisscopeへの参照です。変数thisが、それはその関数内で、以前と同じではありませんのでスコープは、ログイン機能の中に変化している

myapp.controller('LoginController', ['$scope', function(scope) { 
    var that = this; // reference to the correct this context 
    // Users for test 
    this.users = [ 
     { 
      name: 'admin', 
      password: 'admin', 
      role: 'ADMIN' 
     }, 
     { 
      name: 'employee', 
      password: '12345', 
      role: 'EMPLOYEE' 
     } 
    ]; 
    console.dir(this.users); // Prints an array of objects correctly 

    // called when user submits 
    scope.login = function() { 
     console.log('login(). User: '); 
     console.dir(scope.user); // Prints the object with user's input (also correct) 

     var found = false; 
     for(let u of that.users) { // use the correct context here 
      console.log('Comparing with:'); 
      console.dir(u); 

      if(u.name == scope.user.name && u.password == scope.user.password) { 
       console.log('Found!'); 
       found = true; 

       // do something else... 
      } 
     } 

     if(!found) { // show error message... } 
    } 
}]); 
+0

' this.users'を 'scope.users'に変更するだけで、すべてが期待どおりに動くようにすることもできます。 – Brian

+0

それは 'that!= $ scope'ということに注目する価値はありますが、その違いはコントローラですぐには目立たないでしょうが、HTML上にもあります。' this'は '$ scope'に反対しています。 'this 'を使う理由は、' Controller as'構文を使ってHTMLでそれを参照するときにドット表記法を強制することです(ドット表記*を使用しないと*予期しないエラーが発生する可能性があるため) –

+0

'オブジェクトは親オブジェクトを参照します。この場合、 '$ scope'オブジェクトのメソッドの中の' this'は '$ scope'を参照します。 'this'は常に' $ scope'ではなく、この場合は 'this 'です。 – Brian

関連する問題