目前,我正在学习 AngularJS。但是我现在已经在这个问题上浪费了几个小时。
我尝试通过 ngResource 从 AngularJS 调用 WCF 服务。我能够让它与 $http 一起工作,但不能通过 ngResource。
服务很简单:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "flowers", ResponseFormat = WebMessageFormat.Json)]
List<BaseFlower> GetFlowers();
public class Service1 : IService1
{
public List<BaseFlower> GetFlowers()
{
var flowers = new List<BaseFlower>();
flowers.Add(new BaseFlower { Name = "Rose", Price = 1, BoughtOn = DateTime.Now });
flowers.Add(new BaseFlower { Name = "Lilly", Price = 2, BoughtOn = DateTime.Now });
flowers.Add(new BaseFlower { Name = "Heliconder", Price = 3, BoughtOn = DateTime.Now });
return flowers;
}
按照一些示例,我尝试调用 WCF 服务但没有运气,这里是 AngularJs 代码:
var ngFlower = angular.module("ngFlower", ['ngResource']);
ngFlower.factory("Flower", function ($resource) {
return {
flowerData: $resource('http://localhost:61585/Service1.svc/flowers')
}
});
ngFlower.controller("flowerCtrl", function ($scope, Flower) {
$scope.items = Flower.flowerData.query({});
DB = $scope.items;
});
由于我从 $http 切换到“资源”,因此无法再访问 WCF 服务。