2016-12-07 43 views
0

大家好,我正在嘗試綁定angulajs中的表。我從表中的數據庫獲取數據並試圖填充該表。 在這裏,我做了表未在angularjs中綁定

$scope.getPageDetails = function() { 
    $http.get('/api/privilege/getpagedetails/') 
     .success(function (data, status) { 
      $scope.Page = data; 
      console.log($scope.Page); 
     }).error(function() { 
     }); 
} 

然後我得到它被定義爲$scope.Page = [];在這裏$ scope.Page數據我附上我在$ scope.page獲取數據enter image description here

這裏的控制檯圖像是我的表

<table class="table table-bordered table-hover"> 
        <thead> 
         <tr> 
          <th> 
           ID 
          </th> 

          <th> 
           PageName 
          </th> 

          <th> 
           PageUrl 
          </th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr ng-repeat="Page in Pages"> 
          <td>{{Page.id}}</td> 
          <td>{{Page.pageName}}</td> 
          <td>{{Page.pageUrl}}</td> 
          <td> 
           <input type="checkbox" value="Edit" /> 
          </td> 
         </tr> 
        </tbody> 
       </table> 

請幫助我失蹤的地方我將非常感謝大家。

+0

'$ scope.Page'應該是'$ scope.Pages' –

回答

1

在您的觀點中,您正在迭代Pages,但我沒有看到它在您的控制器中定義。

變化

$scope.Page = data; 

$scope.Pages = [data]; 

終極密碼 HTML:

<table class="table table-bordered table-hover"> 
       <thead> 
        <tr> 
         <th> 
          ID 
         </th> 

         <th> 
          PageName 
         </th> 

         <th> 
          PageUrl 
         </th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr ng-repeat="Page in Pages"> 
         <td>{{Page.id}}</td> 
         <td>{{Page.pageName}}</td> 
         <td>{{Page.pageUrl}}</td> 
         <td> 
          <input type="checkbox" value="Edit" /> 
         </td> 
        </tr> 
       </tbody> 
      </table> 

控制器:

$scope.getPageDetails = function() { 
$http.get('/api/privilege/getpagedetails/') 
    .success(function (data, status) { 
     $scope.Pages = [data]; 
    }).error(function() { 
    }); 

}

編輯:如果你的數據是一個對象,使其成爲一個數組,所以NG-重複,可以遍歷它

+0

我應該怎麼做n-repeat –

+0

如果你指向你的控制器中正確的引用(改變'$ scope.Pages = data;'),ng-repeat不需要工作。 Angular將完成其餘的動作 – ocespedes

+0

不開玩笑先生,我將頁面改爲頁面,但在ng-repeat中沒有綁定 –

0

$scope.Page在控制器VS在你的HTML NG重複Pages結合,你需要寫$scope.Pages :)

+0

請先生分享html代碼也 –

0

變化$scope.Page = data$scope.Pages = data

+0

是的,我根據你改變了它,但我應該怎麼做ng-repeat –

1
$scope.getPageDetails = function() { 
    $http.get('/api/privilege/getpagedetails/') 
     .success(function (data, status) { 
      $scope.Pages = data; //change page to pages in whole code, 
           //no need to change any thing in html page 
      console.log($scope.Pages); 
     }).error(function() { 
     }); 
} 
+0

請先生分享html代碼也 –

+0

你在這裏得到的是一個正常的對象與一個鍵和值json。 –

+0

ng-repeat在對象數組上工作.. 在這裏,您可以直接將$ scope與視圖綁定。 –