【问题标题】:How to navigate by posts loaded per json file如何通过每个 json 文件加载的帖子导航
【发布时间】:2015-09-28 22:03:34
【问题描述】:

这是我的 Plnkr:http://plnkr.co/edit/brWn6r4UvLnNY5gcFF2X?p=preview

例如我有一个json文件:

{
  "info": {
     "test1": "test",
     "teste2": "test"
  },

   "posts": [
      {
        "name": "lorem ipsum",
        "content": "sit amet lorem ipsum"
      },
      {
        "name": "Ipsum lorem",
        "content": "sit amet lorem ipsum"
      },
      {
        "name": "lorem ipsum",
        "content": "sit amet lorem ipsum"
      },
      {
        "name": "Sit amet",
        "content": "sit amet lorem ipsum"
      }
   ]
}

在我的控制器中:

var app = angular.module('plunker', ['ngRoute']);

app.controller('MainCtrl', function($scope, $http, $route, $routeParams, $filter) {
  $scope.name = 'Teste';

  $scope.getData = function(){
    $http.get('posts.json')
      .then(function(res){
          $scope.posts = res.data.posts;
          $scope.currentPost = $filter('filter')($scope.posts, {id: $routeParams.id})
          //console.log(res.data.posts);
        });
  };
  //setInterval($scope.getData, 1000);
  $scope.getData();
});

// Routes
app.config(function($routeProvider, $locationProvider) {
  $routeProvider
   .when('/', {
    templateUrl: 'home.html',
    controller: 'MainCtrl',
  })
  .when('/posts/:id', {
    templateUrl: 'post.html',
    controller: 'MainCtrl'
  })
  .otherwise({ redirectTo: '/' });

});

在我的主页视图中:

<div ng-repeat="post in posts">
<a href="./#/posts/{{ post.id  }}">
  <h3>{{ post.name  }}</h3>
  <h5>=> {{ post.content  }}</h5>
</a>
</div>

还有我的帖子视图:

<h6><a href="./#/">Back</a></h6>

<h1>POST ID: {{ currentPost.id }}</h1>
<h2>Name: {{ currentPost.name }}</h2>
<h3>Content: {{ currentPost.content }}</h3>

加载帖子视图时我没有得到值。

谢谢!

【问题讨论】:

  • 包含每个帖子的 id 并在 url 中使用它
  • 好的,但是如何在我的控制器中路由和作用域?
  • 不明白你在问什么。当您的数据中没有 id 开头时,不要从答案中复制新部分来提问
  • 加载视图时,仅将 {{ post.id }} 放在我的 href 参数中不会从我的 json 数组中加载正确的对象。
  • 如果所有数据始终在一个文件中,则需要迭代数组以根据$routeParams找到匹配的id

标签: angularjs multidimensional-array angularjs-scope angularjs-ng-repeat


【解决方案1】:

第一个位置,您应该在每个帖子上添加一些标识符,例如 id 属性,以便在导航到该帖子时,您可以在 URL 中拥有 id 然后使用 ng-href 和 interpolation 来形成导航网址

HTML

<div ng-repeat="post in posts>
  <a ng-href="/post/{{post.id}}" >
   <h1>{{ post.name }}</h1>
   <h1>{{ post.content }}</h1>
  </a>
</div>

编辑

要在帖子页面上显示帖子,您可以使用$routeParams 服务从URL 获取该参数,然后对接收到的数据进行过滤,然后在 HTML 上显示。

app.controller( 'PostsController', function($scope, $http, $routeParams, $filter) {
      $scope.getData = function(){
          $http.get('json/posts.json')
          .then(function(res){
              //for sec of simplicity you could add this data in service.
              $scope.posts = res.data.posts;
              $scope.currentPost = $filter('filter')($scope.posts, {id: $routeParams.id});
            });
      };
      $scope.getData(); //init
});

views/post.html

<div>
  <div> Post with id {{currentPost.id}}</div>
  <div> Name: {{currentPost.name}}</div>
  <div> Content: {{currentPost.content}}</div>
</div>

【讨论】:

  • 嗨!谢谢!我做到了,但我如何确定它的范围以在另一个视图中加载单个内容?
  • 没用,我想是因为我提供的json在json对象数组中没有id参数。我可以从我的控制器生成它吗?
  • @FernandoAureliano 您应该在您的.json 文件中添加它,因为我的第一部分答案确实已经提到了那部分..
  • 是的,但我不能这样做,因为 json 文件是由另一个生成它的服务器提供的,而不是我。=(
  • 我只是手动将 id 放入本地 json 测试文件中以测试导航。就像你说的,链接有效,但是当视图被加载时,它加载的只是 {{currentPost.name}} 和其他参数。此视图已加载到帖子列表所在的同一控制器中。
【解决方案2】:

用这个修复它:

app.controller('MainCtrl', function($scope, $http, $route, $routeParams, $filter) {
  $scope.name = 'Test';

  $scope.getData = function(){
    $http.get('posts.json')
      .then(function(res){
          $scope.posts = res.data.posts;
          $scope.currentPost = $filter('filter')($scope.posts, {id: $routeParams.id});
          $scope.currentPost = $scope.currentPost[0]; // $filter apparently returns an array...
        });
  };
  setInterval($scope.getData, 1000);

  $scope.getData();

});

Plnkr 更新:http://plnkr.co/edit/brWn6r4UvLnNY5gcFF2X?p=preview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2019-12-20
    相关资源
    最近更新 更多