我在此項目中使用了Visual Studio 2015社區版和AngularJS 1.4.7。使用AngularJS 1.4.7服務調用ASP.NET Web API不起作用
ProductAPIController.cs
public class ProductAPIController : ApiController
{
[HttpGet]
public IQueryable<Product> MyProducts()
{
List<Product> p = new List<Product>() { new Product {ProductName ="abc", Id = 1, Description = "Desc 1" },
new Product {ProductName ="def", Id = 2, Description = "Desc 2" },
new Product {ProductName ="ghi", Id = 3, Description = "Desc 3" } };
return p.AsQueryable();
}
}
Product.cs
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
}
app.module.js 我用於應用程序模塊的單獨文件中的最佳實踐here建議
個(function() {
'use strict';
angular.module('app', ['ngCookies', 'ngRoute'])
})();
homeCtrl.js
這裏我使用這兩種服務,並在同一個文件中的控制器。我甚至嘗試在一個單獨的文件中使用它,但它仍然無效。
(function() {
'use strict';
angular.module('app')
.controller('homeCtrl', ['$scope', '$http', 'ProductService', '$location', function homeCtrl($scope, $http, ProductService, $location) {
$scope.products = [];
$scope.getProducts = function() {
ProductService.getProducts().then(function (response) {
$scope.products = response.data;
}, function (err) {
alert(err);
});
};
}])
.service('ProductService', ['$http', function ($http) {
this.getProducts = function() {
return $http.get('/api/ProductAPI');
};
}]);
})();
index.cshtml
這裏我用data-ng-repeat="item in products"
<div class="row" ng-app="app">
<div class="col-md-12" ng-controller="homeCtrl">
<div class="row">
<div class="col-md-12">
<form id="test">
<div data-ng-repeat="item in products">
<div class="col-sm-4 column productbox">
<div class="producttitle">{{item.ProductName}}</div> <div class="action"></div>
<div class="productprice">
<div class="pull-right">
<div >{{item.Id}}</div>
<a href="" class="btn btn-danger btn-sm" role="button"><span class="glyphicon glyphicon-arrow-right"></span> Get Quote</a>
</div>
<div class="pricetext">{{item.Description}}</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@section scripts
{
@Scripts.Render("~/Scripts/angular.js")
@Scripts.Render("~/Scripts/angular-cookies.min.js")
@Scripts.Render("~/Scripts/angular-route.js")
@Scripts.Render("~/Ng/app.module.js")
@Scripts.Render("~/Ng/homeCtrl.js")
}
我嘗試了好幾種不同的方法,但都失敗了。我在這裏做錯了什麼?
編輯
我已經上傳了示例項目GitHub here。下載後,您可能需要重建項目以同步相關的軟件包。
編輯:答案
以供將來參考,在情況下,如果像我這樣的AngularJS初學者看這個問題,答案如下。 基於由@Miyuru Ratnayake給出accepted answer,只是改變了
$scope.getProducts = function() {
......
}
到
function getProducts() {
...
}
,並把它稱爲before the function聲明。單獨的函數activate()不是必需的。
因此控制器是這樣的:
.controller('homeCtrl', ['$scope', '$cookies', '$http', 'ProductService', '$location', function homeCtrl($scope, $cookies, $http, ProductService, $location) {
$scope.products = [];
getProducts();
function getProducts() {
ProductService.getProducts().then(function (response) {
$scope.products = response.data;
}, function (err) {
alert(err);
});
};
}])
你看到任何錯誤?當你從瀏覽器訪問yoursite/api/productapi時會發生什麼,你會得到什麼數據? –
爲什麼要返回IQueryable?只需返回列表。 – Crowcoder
查看瀏覽器網絡控制檯是否有錯誤。最有可能被擊中的端點不正確。 – Chandermani