【问题标题】:Angular pass data to a separate page角度将数据传递到单独的页面
【发布时间】:2013-04-13 02:29:56
【问题描述】:

我有一个使用 ng-repeat 从服务中显示的项目列表。所有这些项目都有一个“完整项目详细信息”链接 (item.html)。将显示更多信息的通用页面。

将点击的项目的数据传递给 item.html 以便显示的信息来自点击的项目的最佳方式是什么?

HTML 看起来像:

<div class="container" ng-app="myApp">
    <div ng-controller="ItemController">
        <div ng-repeat="item in data.details">
            <div class="wrapper">
                <h3>{{item.title}}</h3>
                <p>{{item.description}}</p>
                <a href="#myModal" class="btn" openmodal>Open modal</a>
                <a href="item.html">View full item details</a>
            </div>
        </div>
    </div>
</div>

角度

var myApp = angular.module('myApp', []);

myApp.factory('Data', function(){
    var Data = {};  
    Data.details = [
        {
            "id": 1,
            "title": "Item 1",
            "description": "Post 1 description - Nullam ac tellus sed mi laoreet placerat.",
            "more": "more info"
        },
        {   
            "id": 2,
            "title": "Item 2",
            "description": "Post 2 description - Cociis natoque penatibus et magnis dis parturient montes.",
            "more": "more info"

        },
        {
            "id": 3,
            "title": "Item 3",
            "description": "Post 3 description - Nullam ac tellus sed mi laoreet placerat.",
            "more": "more info"
        }
    ]
    return Data;
});

// Controller
function ItemController($scope, Data){
    $scope.data = Data;
}

提前致谢

编辑

我创建了路线,为每条路线创建了 HTML 页面,然后使用 ng-view 显示相应的内容。

请参阅下面的代码:

// Routes
myApp.config(function($routeProvider){
    $routeProvider
        .when('/', {
                templateUrl:"home.html"
        })
        .when('/task', {
                templateUrl:"item.html"
        })
})

// Controller
function ItemController($scope, $location, Item){
    $scope.data = Item;

    $scope.viewDetail = function(item) {
        $scope.currItem= item;
        $location.path('/item');
    }
}

现在 index.html 看起来像这样:

<div class="container">
    <ng-view></ng-view>
</div>

然后我为我的路线创建了单独的文件:

home.html

<div class="container" ng-app="myApp">
    <div ng-controller="ItemController">
        <div ng-repeat="item in item.details">
            <h3>{{item.title}}</h3>
            <a ng-click="viewDetail(item)">View full item details</a>
        </div>
    </div>
</div>

item.html

<div class="container" ng-app="myApp">
    <div ng-controller="ItemController">
        <h3>{{currItem.title}}</h3>
        <p>{{currItem.description}}</p>
    </div>
</div>

home.html 中的我的链接(查看完整的项目详细信息)使用 ng-click 传递单击的“项目”对象。
现在我想在 item.html 中使用这个 item 对象 console.log(item) 给了我正确的对象。

我尝试在 item.html 中使用“currItem”来显示正确的数据。它不起作用。

不确定我在这里缺少什么?

【问题讨论】:

    标签: jquery json binding model angularjs


    【解决方案1】:

    这实际上取决于您如何构建您的网站以及您希望应用程序如何流动。你有很多选择,第一个是最 Angular 的方式。

    1. 利用the $route servicengView

    2. 将所有内容保持在一个页面上。您需要将&lt;a href="item.html"&gt; 替换为&lt;span ng-click="showDetails(item)"&gt; 之类的东西,这将显示/隐藏一个单独的div,其中包含可以绑定到$scope.currentItem 的item.html 的内容。 $scope.showDetails 看起来像这样:$scope.showDetails = function (item) { $scope.currentItem = item },而 item.html 将位于类似:&lt;div ng-show="currentItem != undefined"&gt;...&lt;/div&gt; 或类似的容器中。

    3. 通过将&lt;a href="item.html"&gt; 更改为&lt;a href="item.html?itemId={{item.id}}"&gt; 向新页面发出请求。这将需要向服务器发出一些额外的请求(不仅要加载 item.html,还要读取 location.search,然后将数据(您在上一个请求中已有的数据)加载回新页面。

    编辑

    如果您要使用 ngView,则必须修改 ItemController 以采用 $route 参数,然后您将使用该参数检查项目 ID,如下所示:

    app.controller('ItemController', function ($scope, $route) {
        // TODO load the item from some service or using ajax, here's the id:
        console.log($route.current.params.id);
    });
    

    【讨论】:

    • 他还可以使用本地存储 (HTML5)。我想如果你加上这个,你会指出所有可能的方式。
    • @Langdon,感谢您的回复。我会采用 Angular 的方式。我还有几个问题。查看我的编辑。
    • @Langdon,考虑到页眉和页脚之间的所有内容都需要替换(其他几页也是如此)。使用 ng-view 还是 ng-template 哪个更好(不确定有什么区别)?
    • @Patrice 是的,如果您不想替换整个页面,这听起来可能会有所帮助。 ngTemplate 的唯一问题(同样,取决于您的实现)是它不像带有单独文件的 ngView 那样模块化。
    • @Patrice 我不确定你的描述。你能在plnkr.co 上设置一些东西来重现你现在所拥有的吗?另外,我发现了ngInclude,这可能对您有所帮助-plnkr.co/edit/ZSPdaqR6WrRh9rbP7vUU?p=preview
    【解决方案2】:

    根据 index.html 的作用,您可以将其作为参数传递。例如

    index.html?title={{item.title}}&id={{item.id}} 
    

    然后在index.html中使用javascript获取查询字符串并解析显示。

    不确定您是否需要将其实现为不同的页面,或者您可以使用不同的视图来代替吗?

    如果它可以作为一个单独的视图实现,那么那里有多个选项:

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2014-05-12
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      相关资源
      最近更新 更多