AngularJs - 无法使用routeProvider

时间:2016-02-24 13:04:10

标签: angularjs route-provider

我从服务器加载一些数据,然后使用ng-repeat更新视图。用户可以编辑另一页面中的数据。现在,是我有点困惑的部分。我可以使用$routeProvided结合$routeParams传递一些参数,让我们说id并根据id从服务器再次获取数据。我相信这是一种更好的方法,比如将整个object传递给下一个控制器,而不是再次调用服务器。那就是我现在正在做的事情:

app.config(function($routeProvider) {
  $routeProvider
    .when('/editPost/:postId/:author', {
      templateUrl: 'editPost.html',
      controller: 'editPostCtrl'
    });
});

app.controller('editPostCtrl', function($scope, $routeParams) {
  console.log($routeParams.author + ", " + $routeParams.postId);
});

现在,我无法使用以下代码提取对象: HTML:

<div ng-repeat="post in posts">
  <button class="btn" ng-click="editPost(post)">Edit</button>
</div>

JS:

app.config(function($routeProvider) {
    $routeProvider
            .when('/editPost/:post', {
        templateUrl : 'editPost.html',
        controller  : 'editPostCtrl'
    });
});

app.controller('editPostCtrl', function($scope, $routeParams) {
  console.log($routeParams.post); //results in [object object]
});

有没有更好的方法来实现这个目标?

0 个答案:

没有答案
相关问题