我有一些文章是从我的API中获得的。当我转到时,我的API正确地列出了它们
http://localhost:60367/api/article/
当我转到时,正确获取单个项目的正确数据
http://localhost:60367/api/article/1
使用angular,如何通过它的id获取其中一篇文章的数据,以便如果我转到我的angular应用程序并单击
http://localhost:60300/perspectives/1/
我得到了那一项的数据。(仅供参考,当我转到索引时
http://localhost:60300/perspectives/
我得到了相应的数据。)
请协助,我的应用程序。js文件如下:
var url = "http://localhost:60367/api/article";
var modules = ['ngRoute', 'ngSanitize'];
var App = angular.module("App", modules);
// Route providers
App.config(function ($routeProvider, $locationProvider) {
$routeProvider
// Get route for perspectives homepage
.when('/', {templateUrl: 'partials/articles-home.html',
controller: ArticleController})
// Get route for perspectives single page
.when("/:id/", {templateUrl: 'partials/articles-single.html',
controller: ArticleController})
.otherwise({ redirectTo : "/"})
// Use the HTML5 History API
$locationProvider.html5Mode({ enabled: true, requireBase: false});
});
// Controller
var ArticleController = function ($scope, $http, $log) {
// For onsuccess, also do console.log for $log property
var onSuccess = function (response) {$scope.articles = response.data;
$log.info(response);};
var onFailure = function (reason) {$scope.error =
reason;$log.info(reason);};
// Get all students and display them in index
var getAllArticles = function () {$http.get(url).then(onSuccess,
onFailure)};
getAllArticles();
// Get single student by Id
//
//
};
App.controller("ArticleController", ArticleController);
解决方案
:
好的,我就是这样解决的,我为单个项目创建了一个新的控制器,并手动编写如下:
var SingleArticleController = function ($scope, $http, $routeParams) {
$http({
url: "http://localhost:60367/api/article/{id}",
params: { id: $routeParams.id },
method: "get"
})
.then(function (response) {
$scope.article = response.data;
});
};