简体   繁体   English

来自angular.js中RouteParam的项目详细信息

[英]item detail from RouteParam in angular.js

I am doing a basic angular example which shows a list view and a detail view. 我正在做一个基本的角度示例,它显示了列表视图和详细视图。 Here is the plunkr: http://plnkr.co/edit/YduJGYQr4lFGK8EXdSXs It currently is showing the detail by using the ng-click="select(page)" in the link rather than the :pageId specified in the RouteParams, so if you used the back button it wouldn't work. 这是plunkr: http ://plnkr.co/edit/YduJGYQr4lFGK8EXdSXs它目前通过链接中的ng-click =“select(page)”而不是RouteParams中指定的:pageId来显示细节,所以如果你使用后退按钮它将无法正常工作。

Basically I know how to get the :pageId from the route param, but not how to select the specific item with it. 基本上我知道如何从路由参数获取:pageId,但不知道如何使用它来选择特定项目。 How would I get the object from a value in the object? 如何从对象中的值获取对象?

While @musically_ut example works, I do not believe the way you are approaching the problem is the recommended way to go about it. 虽然@musically_ut示例有效,但我不相信您接近问题的方式是推荐的方法。 Your approach tightly couples the list view with the details view. 您的方法将列表视图与详细信息视图紧密结合在一起。

A better approach would be to have a dedicated list controller/view, and a dedicated details controller/view. 更好的方法是拥有专用的列表控制器/视图和专用的详细信息控制器/视图。 Inside your details controller you will then use your route param to gain access to the item detail, either via a call to your server, or if you have to have everything in memory, then move your pages array into a service/factory that can be shared by both the list controller and details controller. 在您的详细信息控制器中,您将使用您的路线参数来访问项目详细信息,或者通过调用服务器,或者如果您必须将所有内容都放在内存中,然后将您的页面阵列移动到可以是的服务/工厂中由列表控制器和详细信息控制器共享。 This way both views are decoupled and you could effectively re-use them anywhere you like. 这样两种视图都可以解耦,您可以在任何地方有效地重复使用它们。

As per @charlietfl comment , the official example should point you in the right direction. 根据@charlietfl的评论 ,官方的例子应该指出你正确的方向。

If I understand you correctly, you want to change the route, but do not want to use the URL to determine the state of your application? 如果我理解正确,您想要更改路由,但又不想使用URL来确定应用程序的状态? There is no way of escaping from serializing state in the URL and retreiving it from there because the user may access the app directly from the URL. 无法从URL中序列化状态转义并从中撤消,因为用户可以直接从URL访问应用程序。

This is a working demo which uses angular's routing and $routeParams to save an retrieve the currentPage from the page.linktext : http://plnkr.co/edit/WnJtiqhvUKTKN2PSvwtc?p=preview 这是一个工作演示,它使用angular的路由和$routeParams来保存从page.linktext检索currentPagehttppage.linktext

Here are the changes with comments. 以下是评论的更改。

Template 模板

  <!-- language: kept the `appCtrl` outside so that it controls all the
       templates loaded in place of 'ng-view'-->
  <div class="container" ng-controller="appCtrl">
    <div class="row">
        <div class="span12">
            <div id="messagesDiv">
                <ul>
                    <!-- have removed the `ng-click` -->
                    <li ng-repeat="page in pages"><a href="#/pages/{{page.linktext}}">{{page.linktext}}</a></li>
                </ul>

                <!-- moved the content of the partials to content.html -->
                <div ng-view></div>
        </div>
    </div>
</div>

Module configuration 模块配置

app.config(function($routeProvider){
$routeProvider.
    when('/pages/:pageId', {templateUrl:'content.html'}).
    when('/content', {template: '<div>No link selected</div>'}).
    otherwise({redirectTo:'/content'}); // <-- `redirectTo` cannot take a template

Controller 调节器

$scope.pages = [

    {
        linktext: 'first',
        title: 'first title',
        content: 'first content'
    },
    {
        linktext: 'second',
        title: 'second title',
        content: 'second content'
    }

];

// Set up a watch to notice whenever the user clicks a link and route changes
$scope.$watch(function () { return $routeParams.pageId }, function (newVal) {

  if (typeof newVal === 'undefined') return;

  // Pick the content for the page using the `linktext` of the pages and `pageId`
  $scope.currentPage = $scope.pages.filter(function (p) { 
    return p.linktext === newVal;
  })[0];
});

When you select the object, you can put the object in a holder service, and adjust the location path. 选择对象时,可以将对象放入持有者服务中,并调整位置路径。 The router will then take you to the detail. 然后路由器将带您进入详细信息。 In the detail's controller, you then simply pick the object from the 'holder' service. 在详细信息的控制器中,您只需从“持有者”服务中选择对象即可。

So for example: 例如:

snippet from my 'list' view: 我的“列表”视图中的代码段:

... some stuff here...

<tr ng-repeat="msg in msgs">
   ... and here ...

   ... here's an edit link that takes you to the detail page ...
   <a href="" ng-click="edit(msg)">
     <span class="glyphicon glyphicon-edit"/>
   </a>
</tr>

my controller behind the list of objects: 我的控制器在对象列表后面:

     $scope.edit = function (msg) {
        Holder.msg(msg);
        $location.path('/messages/' + msg.id);
    };

my service: 我的服务:

.service('Holder', function () {

    var _data;

    return {

        msg: function (msg) {
            if (msg) {
                _data = msg;
            }
            else {
                return _data;
            }
        }
    }
})

my detail controller: 我的细节控制器:

    .controller('MsgCtrl', function ($scope, $location, $routeParams, Holder) {

    //...

    $scope.msg= $routeParams.msgId ? Holder.msg() : {};

    //...

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM