2016-03-29 26 views
0

我有一个由'catID'和'catName'组成的Json字符串。到目前为止,我已经设法创建了一个按钮列表,标有每个类别。当用户点击与类别关联的按钮时,我一直试图返回catID。它现在返回$ index,这很好,但理想情况下,catID可以让我将信息传递到我的编码的下一个阶段。我需要$索引来激活正确的按钮。需要从ng-repeat按钮列表中返回一个变量

这是我的时刻:

<h4>Select Category</h4> 
    <ul class="nav nav-pills"> 
     <li ng-repeat="categories in model" ng-class="{'active':rowIndex == $index }" ng-click="selectRow($index)"> 
     <a href="">{{categories.catID + " " + categories.catName}}</a> 
     </li> 
    </ul> 

我的角度控制器:

app.controller("catBtnCtrl", function ($scope) { 
     $scope.selectRow = function (index) { 
      if (index === $scope.rowIndex) 
       $scope.rowIndex = -1; 
      else 
       $scope.rowIndex = index; 
     }; 
    }); 

我已经尝试了几种不同的方法,但都是一片空白。任何帮助,将不胜感激!

回答

1

这是怎么回事: 当你获得控制器功能的索引后,你可以搜索正确的项目细节。 或者你也可以通过2个值的功能和HTML给他们

UPDATE

app.controller("catBtnCtrl", function ($scope) { 
     $scope.categoryID = null; 
     $scope.categoryName = null; 
     $scope.selectRow = function (index, id, name) { 
      if (index === $scope.rowIndex) 
       $scope.rowIndex = -1; 
      else 
       $scope.rowIndex = index; 
      $scope.categoryID = id; 
      $scope.categoryName = name; 
      // next do some functions that retrieve the data that corresponds to these parameters 
     }; 
    }); 
选择类别
<ul class="nav nav-pills"> 
    <li ng-repeat="categories in model" ng-class="{'active':rowIndex == $index }" ng-click="selectRow($index, categories.catID, categories.catName)"> 
    <a href="">{{categories.catID + " " + categories.catName}}</a> 
    </li> 
</ul> 
+0

你能提供一个你的假设的例子吗? –

+0

是的。我已经更新了我的回答 – dpaul1994

+0

谢谢,我真的很感激。也帮助我更多地了解JavaScript。 –

0

您也可以通过整个项目能:

<li ng-repeat="categories in model" ng-class="{'active':rowIndex == $index }" ng-click="selectRow(categories, $index)"> 
    <a href="">{{categories.catID + " " + categories.catName}}</a> 
</li> 

但是你需要改变这样的参数:

$scope.selectRow = function (category, index) {} 
+0

这看起来像我想要做的。然而,我目前在JavaScript/AngularJS方面的知识非常少。我觉得除此之外还有更多。虽然我可能是错的! –