2016-12-17 6 views
1

Cannot read property 'active' of undefinedCannot read property 'boss' of undefinedという2つのエラーがありますが、私はユーザーの配列に対してng-repeatを使用しようとしています。それは正しい方法でテーブルを出力しますが、またng-repeatエラー。定義されていないプロパティを読み取ることができません

<thead> 
     <tr> 
      <th>#</th> 
      <th>Username</th> 
      <th>Birthday</th> 
      <th>City</th> 
      <th>Active</th> 
      <th>Boss</th> 
     </tr> 
    </thead> 
    <tbody> 
     <div> 
      <tr ng-click="goToProfile(user.id)" ng-repeat="user in users"> 
       <td>{{user.id}}</td> 
       <td>{{user.name}}</td> 
       <td>{{user.birthday}}</td> 
       <td>{{user.city}}</td> 
       <td> 
        <input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }"> 
       </td> 
       <td> 
        <input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }"> 
       </td> 
      </tr> 
     </div> 
    </tbody> 

終わりに:私は次のコードしているビューで

  $scope.users = [ 
      { 
       id: 1, 
       name: 'John', 
       birthday: '06.07.2008', 
       city: 'Budapest', 
       active: false, 
       boss: false 
      }, 
      { 
       id: 2, 
       name: 'Mary', 
       birthday: '01.02.2003', 
       city: 'Berlin', 
       active: false, 
       boss: true 
      }, 
      { 
       id: 3, 
       name: 'James', 
       birthday: '04.05.2006', 
       city: 'Vienna', 
       active: false, 
       boss: false 
      } 
     ]; 

     $scope.isActive = function (id) { 
      return $scope.users[id].active; 
     } 

     $scope.isBoss = function (id) { 
      console.log($scope.users[id]); 
      return $scope.users[id].boss; 
     } 

:私は、次のコードを使用していコントローラで

それらのエラーをスローします。私は何か見落としてますか?

<tbody> 
    <!-- ngRepeat: user in users --> 
    <tr ng-click="goToProfile(user.id)" ng-repeat="user in users" class="ng-scope"> 
     <td class="ng-binding">1</td> 
     <td class="ng-binding">John</td> 
     <td class="ng-binding">06.07.2008</td> 
     <td class="ng-binding">Budapest</td> 
     <td> 
      <input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }" checked="checked"> 
     </td> 
     <td> 
      <input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }" checked="checked"> 
     </td> 
    </tr> 
    <!-- end ngRepeat: user in users --> 
    <tr ng-click="goToProfile(user.id)" ng-repeat="user in users" class="ng-scope"> 
     <td class="ng-binding">2</td> 
     <td class="ng-binding">Mary</td> 
     <td class="ng-binding">01.02.2003</td> 
     <td class="ng-binding">Berlin</td> 
     <td> 
      <input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }" checked="checked"> 
     </td> 
     <td> 
      <input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }" checked="checked"> 
     </td> 
    </tr> 
    <!-- end ngRepeat: user in users --> 
    <tr ng-click="goToProfile(user.id)" ng-repeat="user in users" class="ng-scope"> 
     <td class="ng-binding">3</td> 
     <td class="ng-binding">James</td> 
     <td class="ng-binding">04.05.2006</td> 
     <td class="ng-binding">Vienna</td> 
     <td> 
      <input type="checkbox" name="active" ng-checked="{ checked: isActive(user.id) }"> 
     </td> 
     <td> 
      <input type="radio" name="boss" ng-checked="{ checked: isBoss(user.id) }"> 
     </td> 
    </tr> 
    <!-- end ngRepeat: user in users --> 

</tbody> 
+1

してくださいに変更(undefinedがある$scope.users配列の4番目の項目を表す)は、インデックス3がありません;この質問に回答した場合は、その答えを受け入れて、新しい問題の新しい質問を開きます(必要に応じてこの質問を参照)。スレッドの根本的な問題を変更すると、将来の読者はどの答えがどの問題を解決したのかを特定することが非常に困難になります。 – Claies

答えて

2

あなたisActiveisBoss機能がuser.idを渡されているためです。

はまた、ここで結果をレンダリングしています。あなたのuser.idがid 1から始まり、これらの関数では、渡されたインデックスを使って$scope.usersにアクセスしようとしています。しかし、実際に

別の質問を追加するための質問を編集しないでください、あなたの..isActive(user.id)...isActive($index)にし、あなたの...isBoss(user.id)...isBoss($index)

+0

ありがとうございました。私はポストの編集を行って、私が残したいくつかのバグを追加しました。あなたを見てください。 –

+1

更新プログラムの別の問題を開く必要があります。最初の質問とは無関係です – Ladmerc

関連する問題