2017-04-26 4 views
0

私はAnguarJsからwebAPIコールをしようとしていました。私は成功した応答 "データ"オブジェクトを完全に受け取ります。 "ng-repeat"のHTMLページに渡されると、レコードは表示されません。続きanglejsを使ってコレクションの値を繰り返せないng-repeat

 <tr ng-repeat = "cust in Customers"> 
      <td>{{ cust.CustomerName }} </td> 
      <td>{{ cust.CustomerCode }}</td> 
      <td>{{ cust.CustomerAmount }}</td> 
      <td>{{ cust.ProcessDate }}</td> 
     </tr> 

を働いていないものですが、私はこのように置けば、それは、0番目のインデックスレコード

<tr ng-repeat = "cust in Customers"> 
    <td>{{cust[0].CustomerName }} </td> 
    <td>{{cust[0].CustomerCode }}</td> 
    <td>{{cust[0].CustomerAmount}}</td> 
    <td>{{cust[0].ProcessDate }}</td> 
</tr> 

注意が表示されます。以下のコードでは、私は別の内のファイルを分割しましたjavascriptファイルを参照し、メインのhtmlページで参照してください。

マイフィドルリンク:JsFiddle

決意でそれを助けてください。

function Utility(){ 
 
    this.ApplicationVersion = "0.0.1"; 
 
    this.ApplicationName = "AngularJs First Project"; 
 
    this.getDate = function() { 
 
     var dt = new Date(); 
 
     return dt.toDateString(); 
 
    } 
 
    this.IsEmpty = function (value) { 
 
     if (value.length == 0) { 
 
      return false; 
 
     } 
 
     else { 
 
      return true; 
 
     } 
 
    } 
 
} 
 

 
function Customer(utility) { 
 
    this.CustomerCode = "1001"; 
 
    this.CustomerName = "Ragu"; 
 
    this.CustomerAmount = 100; 
 
    this.CalculateDiscount = function() 
 
    { 
 
     return 10; 
 
    } 
 
    this.ProcessDate = utility.getDate(); 
 

 
} 
 

 
function Factory() 
 
{ 
 
    return { 
 
     CreateCustomer: function (utility) { 
 
      return new Customer(utility); 
 

 
     } 
 
    } 
 
} 
 
    
 
/// <reference path="Utility.js" /> 
 
/// <reference path="Customer.js" /> 
 
var myApp = angular.module("myApp", []); 
 

 
myApp.controller("BindingCode",BindingCode); 
 
myApp.factory("Factory", Factory); 
 
myApp.service("UtilityObj", Utility); 
 
function BindingCode($scope, UtilityObj, Factory,$http) 
 
{ 
 
    $scope.Customer = Factory.CreateCustomer(UtilityObj); 
 
    $scope.Customers = []; 
 
    $scope.Utility = UtilityObj; 
 
    $scope.Customer.CustomerCode = "1002"; 
 
    $scope.Customer.CustomerName = "Raman"; 
 
    $scope.Customer.ProcessDate = UtilityObj.getDate(); 
 
    $scope.Color = "blue"; 
 
    $scope.$watch("Customer.CustomerAmount", function() { 
 
     if ($scope.Customer.CustomerAmount < 1000) { 
 
      $scope.Color = "Red"; 
 
     } 
 
     else { 
 
      $scope.Color = "Green"; 
 
     } 
 
    }); 
 
    $scope.Submit = function() 
 
    { 
 
     debugger 
 
     if ($scope.Utility.IsEmpty($scope.Customer.CustomerAmount)) { 
 
      debugger 
 
      
 
      $http.post("http://localhost:61860/api/Customer", $scope.Customer).then(function(data){ 
 
       $scope.Customers = data; 
 
       debugger 
 
        $scope.Customer = {}; // clearing the record 
 
      }, 
 
      function(data) 
 
      { 
 
       debugger 
 
       alert("inside error http call" + data); 
 
      } 
 
      ); 
 
      
 
      //$http.post("http://localhost:61860/api/Customer", $scope.Customer). 
 
      // success(function (data) { 
 
      // debugger 
 
      // $scope.Customers = data; 
 
      // $scope.Customer = {}; 
 
      //}); 
 
     } 
 
     else { 
 
      alert("No Proper Date"); 
 
     } 
 
    } 
 
}
<script src="Scripts/angular.js"></script> 
 
<script src="Customer.js"></script> 
 
<script src="Utility.js"></script> 
 
<script src="app.js"></script> 
 
<body ng-app="myApp"> 
 
    <div id="CustScreen" ng-controller="BindingCode"> 
 
     CustomerCode : <input type="text" ng-model="Customer.CustomerCode" id="txtCustomercode" /> <br /> 
 
     CustomerName : <input type="text" ng-model="Customer.CustomerName" id="txtCustomerName" /> <br /> 
 
     CustomerDate : <input type="text" ng-model="Customer.ProcessDate" id="txtCustomerDate" /> <br /> 
 
     CustomerAmount : <input type="text" style="background-color:{{ Color }}" ng-model="Customer.CustomerAmount" id="txtCustomerAmount" /><br /> 
 
     <br /> 
 
     {{ Customer.CustomerCode }} <br /> 
 
     {{ Customer.CustomerName }} <br /> 
 
     {{ Customer.ProcessDate }}<br /> 
 
     {{ Customer.CustomerAmount}} <br /> 
 
     <input type="submit" ng-click="Submit()" id="Submit" /> 
 
     <table> 
 
      <tr> 
 
       <td>Name</td> 
 
       <td>Code</td> 
 
       <td>Amount</td> 
 
       <td>ProcessDate</td> 
 
      </tr> 
 
      <tr ng-repeat = "cust in Customers"> 
 
       <td>{{cust.CustomerName }} </td> 
 
       <td>{{cust.CustomerCode }}</td> 
 
       <td>{{cust.CustomerAmount}}</td> 
 
       <td>{{cust.ProcessDate }}</td> 
 
      </tr> 
 
      </table> 
 
</div>  
 
</body> 
 

+0

jsfiddleのポイントは、問題を再現することです。質問と同じコードを貼り付けるだけではありません。あなたは、他のファイルから必要最小限のコードを含める必要があり、場合によっては外部ソースから角度をロードする必要があります。あなたのバックエンドから返されたjsonがどのように見えるかを私たちに見せてもらえますか?すべてのCustアイテムが配列であるようです - 配列の配列を返していますか? –

+0

また、付属のjsオーダーを変更して試すこともできます。あなたのコードでangular.min.jsはどこですか? – coderwill

+0

これは私が推測するのを助けることができる.. [http://stackoverflow.com/questions/31748612/angularjs-ng-repeat-array-of-arrays](http://stackoverflow.com/questions/31748612/angularjs- ng-repeat-array-of-arrays) – SanjanaHE

答えて

0

試してみてください。この $ scope.Customers =データ[0];

+0

"$ scope.Customers = data [0];"それでも、それは – ragunath79

+0

をリストされていない私にはわからない理由を次のよう に動作していない< "お客様にCUST" = TR NGリピートを> ​​{{}} cust.CustomerName ​​{{} cust.CustomerCode } {{cust.CustomerAmount}} }} ProcessDateしかし、私はこのように置けば、それは の 0番目のインデックスレコードが表示されますが ​​{{カスト[0] .CustomerName}} ​​{{カスト[0] .CustomerCode}} ​​{{カスト[0] .CustomerAmount}} ​​{{カスト[0] .ProcessDate}} – ragunath79

+0

はあなたがここにあなたが 'となっているものを投稿できます$ httpからの 'データ'? – himanshu

関連する問題