2014-01-14 48 views
0

我使用在我所填充使用的数据在数据库中,我写一个网页的方法来得到这样无法绑定JSON在角JS

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static string getname() 
    { 
     SqlHelper sql = new SqlHelper(); 

     DataTable dt = sql.ExecuteSelectCommand("select cust_F_name,cust_L_name from customer"); 

     Dictionary<string, object> dict = new Dictionary<string, object>(); 
     object[] arr = new object[dt.Rows.Count]; 
     List<CustName> custName = new List<CustName>(); 
     for (int i = 0; i <= dt.Rows.Count - 1; i++) 
     { 
      CustName c = new CustName(); 
      c.cust_F_name = dt.Rows[i]["cust_F_name"].ToString();    
      custName.Add(c); 

     } 
     dict.Add("JsonCustomer", custName); 
     JavaScriptSerializer json = new JavaScriptSerializer(); 
     return json.Serialize(dict); 
     //return "Rhsuhikesh"; 

    } 

} 
public class CustName 
{ 
    public string cust_F_name { get; set; } 
} 
数据的客户名单角JS开发的应用程序列表

听清楚JSON数据作为

var DemoApp = angular.module('DemoApp', []); 

DemoApp.factory('SimpleFactory', function ($http) { 
    return { 
     getCustomer: function() { 
      return $http.post('Home.aspx/getname', { name: "" }); 
     } 
    }; 
}); 

DemoApp.controller('SimpleController', function ($scope, SimpleFactory) { 
    SimpleFactory.getCustomer().then(function (customer) { 
     $scope.Customer = customer.data; 
    }, function (error) { 
     // error handling 
    }); 
}); 

,并认为这是

<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="DemoApp"> 
<head runat="server"> 
    <title></title> 
</head> 
<body data-ng-controller="SimpleController"> 
    <form id="form1" runat="server"> 
    <div> 
     Name<input type="text" data-ng-model="Name" />{{ Name }} 
     <ul> 
      <li data-ng-repeat="customerName in Customer | filter:Name">{{ customerName }} </li> 
     </ul> 
    </div> 
    </form> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
    <script src="Script/Home.js" type="text/javascript"></script> 
</body> 
</html> 

和我得到O/P为

outPut

,但我想它作为

以JSON

Accepted output

数据我在名称值对来访问,但我不知道如何做到这一点请大家帮帮我出去如果它。

在此先感谢。

+0

对我来说,你的回应是JSON序列化两次。 – Stewie

回答

0

既然你得到的结果JSON字符串你需要使用angular.fromJson

例如将其转换为JavaScript对象:

DemoApp.controller('SimpleController', function ($scope, SimpleFactory) { 
SimpleFactory.getCustomer().then(function (customerData) { 
    var customersRawObject = angular.fromJson(customerData); 
}, function (error) { 
    // error handling 
});}) 

然后,你可以做财产以后这样的:

$scope.customerA=customersRawObject.JsonCustomer[0];