gpt4 book ai didi

javascript - 在另一个 ng-route 页面上显示数组中的特定对象

转载 作者:行者123 更新时间:2023-11-28 04:28:37 25 4
gpt4 key购买 nike

我正在构建一个项目,其中有一堆来自数组的配置文件。一个页面将它们显示为列表,其中每个配置文件都有一个按钮来显示有关它的更多详细信息。

问题是我不知道如何将这个特定的对象数据传递到另一个路由。所以我想知道如何做到这一点。有什么建议么?谢谢

Controller .js

var App = angular.module("App", ['ngRoute']);

App.config(function($routeProvider) {
$routeProvider

.when('/allprofiles', {
templateUrl : 'pages/all.profiles.html',
controller : 'allProfilesController'
})

.when('/profile/:id/', {
templateUrl : 'pages/profile.html',
controller: 'profileController'
})

.otherwise({redirectTo:'/allprofiles'});
});

App.factory('Allprofiles', function(){
return [
{name: "Adam", role: "Photographer", photo: "img/team/2.jpg", id:"1"},
{name: "John", role: "Musician", photo: "img/team/3.jpg", id:"2"},
{name: "Sam", role: "Actor", photo: "img/team/1.jpg", id:"3"},
{name: "Rachel", role: "Web Developer", photo: "img/team/3.jpg", id:"4"},
{name: "Joe", role: "Dancer", photo: "img/team/2.jpg", id:"5"},
{name: "Francis", role: "Psychology", photo: "img/team/1.jpg", id:"6"}
]
})

App.controller('allProfilesController', function($scope, $route, $location, Allprofiles) {
$scope.profiles = Allprofiles;
})

App.controller('profileController', function($scope, $route, $location) {

})

最佳答案

您可以使用$routeParams

检索第二个 Controller 中的 routerParams,

app.controller("profileController", function($scope, $routeParams) {
$scope.id = $routeParams.id;
$scope.name = $routeParams.name;
});

演示

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

app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
template: "HI this is home Screen",
controller: 'allProfilesController'
})
.when('/profile/:id', {
templateUrl: "template.html",
controller: 'profileController'
})
.otherwise({
redirectTo: '/home'
})
}]);

app.controller("profileController", function($scope, $routeParams) {
$scope.id = $routeParams.id;
$scope.name = $routeParams.name;

});

app.factory('Allprofiles', function(){
return [
{name: "Adam", role: "Photographer", photo: "img/team/2.jpg", id:"1"},
{name: "John", role: "Musician", photo: "img/team/3.jpg", id:"2"},
{name: "Sam", role: "Actor", photo: "img/team/1.jpg", id:"3"},
{name: "Rachel", role: "Web Developer", photo: "img/team/3.jpg", id:"4"},
{name: "Joe", role: "Dancer", photo: "img/team/2.jpg", id:"5"},
{name: "Francis", role: "Psychology", photo: "img/team/1.jpg", id:"6"}
]
})
app.controller('allProfilesController', function($scope, $route, $location, Allprofiles) {
$scope.profiles = Allprofiles;
})

<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="demo" ng-controller="allProfilesController">

<h3>Hello, </h3>
<div ng-repeat="profile in profiles">
<ul>
<li>{{profile.name}}</li>
<li><a href="#profile/{{profile.id}}">Detail</a></li>
</ul>
</div>
<hr/>
<h1 ng-view=""></h1>
</body>

</html>

关于javascript - 在另一个 ng-route 页面上显示数组中的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com