Javascript 角度:从ng repeat传递对象数组的索引以访问对象数据
我用ng repeat显示了一组对象:Javascript 角度:从ng repeat传递对象数组的索引以访问对象数据,javascript,html,angularjs,Javascript,Html,Angularjs,我用ng repeat显示了一组对象: <div class = "row" ng-repeat="article in academicArticles"> <div class = "col-md-9 academicArticle" ng-click = "index = $index"> <a ui-sref ="article"> <h2> {{article.title}} </h2
<div class = "row" ng-repeat="article in academicArticles">
<div class = "col-md-9 academicArticle" ng-click = "index = $index">
<a ui-sref ="article">
<h2> {{article.title}} </h2>
<p> {{academicArticles.indexOf(article)}} </p>
<img src = "{{article.img}}" class = "img-responsive">
</a>
</div>
</div>
{{文章标题}
{{学术文章索引(文章)}
我想在另一个状态下显示所选的“文章”。现在我有:
<div class ="container">
<div class = "jumbotron">
<img src = "../../../photos/danielpark.jpg">
<h1>{{academicArticles[index].title}}</h1>
<p> Written By: {{academicArticles[index].author}} </p>
</div>
</div>
{{学术论文[索引].title}
作者:{{学术论文[索引].作者}
没有错误,但
{{academicArticles[index].author}
和{{academicArticles[index].title}
为空。如何在另一个状态访问所选文章的数据?可以通过将所选“article”的索引作为状态参数传递给另一个状态来实现这一点。
假设另一个状态称为state2,您需要将state参数添加到路由器注册中的状态,例如,如果您像这样装配了新状态,您可以如下添加索引
$stateProvider.state("state2", {
url: '/state2/:index',
templateUrl:'state2.html',
controller: 'state2Controller'});
然后在state2Controller中,您可以使用stateParams服务访问索引参数,如下所示:
(function() {
angular
.module("app")
.controller("state2Controller", state2Controller);
state2Controller.$inject = ["$scope", "$stateParams"];
function state2Controller($scope, $stateParams){
$scope.index = $stateParams.index;
}
})()
您可以通过将所选“article”的索引作为状态参数传递给另一个状态来实现这一点。 假设另一个状态称为state2,您需要将state参数添加到路由器注册中的状态,例如,如果您像这样装配了新状态,您可以如下添加索引
$stateProvider.state("state2", {
url: '/state2/:index',
templateUrl:'state2.html',
controller: 'state2Controller'});
然后在state2Controller中,您可以使用stateParams服务访问索引参数,如下所示:
(function() {
angular
.module("app")
.controller("state2Controller", state2Controller);
state2Controller.$inject = ["$scope", "$stateParams"];
function state2Controller($scope, $stateParams){
$scope.index = $stateParams.index;
}
})()
我认为您被
ng repeat
范围问题困住了,诀窍不是直接将基本类型绑定到范围,而是使用一些对象,如$scope.model
和$scope.model.authorId
我认为您被ng repeat
范围问题困住了,诀窍不是直接将基本类型绑定到范围使用一些对象,如$scope.model
和$scope.model.authord
最简单的方法是使用url参数在url中传递文章id,在其他状态下使用id获取该文章。For链接类似于ui sref=“article({id:article.id})”
最简单的方法是使用url参数在url中传递文章id,在其他状态下使用id获取该文章。For链接类似于ui sref=“article({id:article.id})”
谢谢!有没有办法将整个对象作为状态参数传递?比如url:“/state2/:article”,你不能直接传递洞对象,但你可以随意添加参数,你需要将状态更新为$stateProvider.state(“state2”,{url:'/state2/:index?param1¶m2¶m3',然后在ui-serf中可以像ui-sref=“article({index:article.index,param1:article.params1})”一样使用它谢谢!是否有任何方法可以将整个对象作为状态参数传递?类似于url:“/state2/:article”,您不能直接传递洞对象,但您可以随意添加参数,您需要更新状态,使其类似于$stateProvider.state(“state2”,{url:'/state2/:index?param1¶m2¶m3',然后在ui-serf中可以像ui-sref=“article({index:article.index,param1:article.params1})”一样使用它