gpt4 book ai didi

javascript - 对 id 的 Angular 点击应该路由到不同的页面

转载 作者:行者123 更新时间:2023-11-30 16:12:12 25 4
gpt4 key购买 nike

嗨,我是 Angular 的新手,我有 json 数据。所以当我点击从 json 获得的 Id 时,它应该路由到不同的页面。我该怎么办

Head.html

  </head>

<body ng-app="MyApp" ng-controller="MyController">

<table border="1" style="width:100%">
<tr >
<td href=#home>id</td>
<td>first_name</td>
<td>dateBirth</td>
<td>currentCountry</td>
</tr>
<tr ng-repeat="x in list">
<td><a href="name.html">{{x.id}}</a></td>
<td>{{x.first_name}}</td>
<td>{{x.dateBirth}}</td>
<td>{{x.currentCountry}}</td>
</tr>
</table>


</body>

脚本.js

var app= angular.module("MyApp", []);

app.controller('MyController', function($scope,$http) {
$http({
method: "GET",
url: "http://192.168.1.1:8080/administrator/"
}).then(function mySucces(response) {
$scope.list = response.data;
console.log($scope.list)
}, function myError(response) {
$scope.myWelcome = response.statusText;
});

});

我的笨蛋 http://plnkr.co/edit/Gpmsc7pb1gfWx3EgKk6s?p=preview

感谢任何帮助

最佳答案

使用$routeProvider$stateProviderng-clickui-sref 来实现通过 id导航。我将通过一个使用 $stateProvider 的简单示例。

我将有 3 个 views 来模拟您提供的内容:

  • header.html:,其中包含您的 ng-repeat 以显示表格数据,我们可以通过单击 ID 进行导航。
  • first.html:如果用户单击 id 列中的 1,则会导航到 first.html
  • second.html:如果用户点击 id 列中的 2,它将导航到 second.html

你的 JS:

var app = angular.module('nested', ['ui.router']);
app.config(function($stateProvider,$locationProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider.state('top', {
url: "",
templateUrl: "header.html",
controller: 'MyController'
})
.state('first', {
url: '/app/:id',
templateUrl: "first.html",
controller: 'FirstController'
})
.state('second', {
url: '/app/:id',
templateUrl: "second.html",
controller: 'SecondController'
})

});

app.controller('MyController', function($scope,$state) {
/*//commented this part so that you can work on it later.
$http({
method: "GET",
url: "http://192.168.1.134:8080/administrator/%20ApplicationList.json"
}).then(function mySucces(response) {
$scope.list = response.data;
console.log($scope.list)
}, function myError(response) {
$scope.myWelcome = response.statusText;
});
*/
$scope.list = [{'id':1,'first_name':'alex','dateBirth':'18th march','currentCountry':'Nepal'},
{'id':2,'first_name':'rumba','dateBirth':'18th march','currentCountry':'USA'}
]

$scope.navigate = function(id){
if(id==1){
$state.go('first',{id:1});
}
else if(id==2){
$state.go('second',{id:2});
}
}


});


app.controller('FirstController', function($scope,$state) {
console.log(JSON.stringify($state.params));
$scope.params = $state.params;
});

app.controller('SecondController', function($scope,$state) {
console.log(JSON.stringify($state.params));
$scope.params = $state.params;
});

在这个 PLUNKER 示例中,我什至提供了如何通过 controller 传递 params:
http://plnkr.co/edit/n8p7ycV7k5rsn1f3bQcT?p=preview

关于javascript - 对 id 的 Angular 点击应该路由到不同的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36078742/

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