2016-12-31 40 views
1

我有納克重複顯示的對象的數組:角:使對象數組的索引從NG-重複訪問對象數據

<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> 

沒有錯誤,但{{academicArticles[index].author}}{{academicArticles[index].title}是空白。我如何在另一個狀態訪問所選文章的數據?

+0

最簡單的方法是使用url參數傳遞url中的文章ID,並在其他狀態下使用該ID獲取該文章。對於鏈接將像'ui-sref =「article({id:article.id})」' – charlietfl

回答

0

您可以通過將所選「article」的索引作爲狀態參數傳遞給另一個狀態來完成此操作。 假設其他狀態調用狀態2,您需要將狀態參數添加到狀態路由器註冊,例如,如果你rigstered喜歡這個新的狀態,您可以添加指數跟隨

$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; 

      } 
     })() 
+0

謝謝!有沒有辦法將整個對象作爲狀態參數傳遞?例如url:'/ state2 /:article', –

+0

你不能直接傳遞hole對象,但是你可以根據需要添加參數,你需要更新狀態如下所示$ stateProvider.state(「state2」 ,{ url:'/ state2 /:index?param1&param2&param3'然後在ui-serf中可以像這樣使用ui-sref =「article({index:article.index,param1:article.params1})」 –

0

我想你是ng-repeat範圍問題所困,訣竅是不綁定的原始類型的範圍,而不是直接使用一些對象像$scope.model$scope.model.authorId