【发布时间】:2018-11-19 13:40:55
【问题描述】:
也许有人会帮助我。我在 angularjs 中编写了一个应用程序,我有一个名为 list.html 的文件,它从 jsonplaceholder 检索帖子列表并列出它们,并带有指向帖子详细信息的链接。在 $ routeParams 中,我传递所选的 id 并将其拾取。不幸的是,我不知道如何下载帖子的详细信息并将它们显示在 details.html 文件中。例如,如果我想删除某些内容,我将 $scope.deletePost 写为一个函数并给出一个 id,但我不知道如何列出详细信息。
//routing.js
var myApp = angular.module('myApp', ["ngRoute"])
myApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/test', {
templateUrl: '/event/example.html',
controller: 'exampleController'
}, null)
.when('/list', {
templateUrl: '/event/list.html',
controller: 'exampleController'
}, null)
.when('/test-list', {
templateUrl: '/test/list.html',
controller: 'testController'
}, null)
.when('/test/:id', {
templateUrl: '/test/details.html',
controller: 'testController'
}, null)
}
]);
//controller.js
angular.module('myApp').controller('testController', function ($scope, $http, $routeParams) {
$http.get('https://jsonplaceholder.typicode.com/posts').then(function (response) {
$scope.posts = response.data;
});
$scope.id = $routeParams.id;
});
//details.html
<div data-ng-controller="testController">
{{data}}
</div>
//list.html
<div data-ng-controller="testController">
<ul>
<li ng-repeat="post in posts">
Tytuł: {{post.title}} <a href="#!test/{{post.id}}" >Show</a>
</li>
</ul>
</div>
【问题讨论】:
标签: javascript angularjs ngroute angularjs-http