2016-06-11 137 views
0

我试图使用嵌套的ng-repeat和第二个我需要根据我以前设置ng-model筛选。但在第二个ng-repeat我需要放置一个ng-if取决于字段名称。ng-repeat:如何访问字段名称

事情是,我试图使用(键,价值)形式(它完美地工作),但它非常烦人的应用过滤器。所以我想知道是否有方法访问名称字段。

例如:

<tbody> 
    <tr ng-repeat="item2 in items2"> 
    <td ng-repeat="val in item2 | filer:blabla" ng-if="val.field != 'foto'">{{val}}</td> 
    </tr> 
</tbody> 

回答

0

理想情况下,这个过滤器代码不应该坐在你的视图(html文件)中,而应该在你的控制器中作为函数由ng-show/ng-if函数。=将从您的视图中调用。

0

你可能最终有一个函数评估的结果,并使用Object.keys你可以得到字段的名称,并进行比较。

由于angular会遍历对象的值,因此您需要索引ng-repeat来获取实际字段。

在HTML:

<tbody> 
    <tr ng-repeat="item2 in items2"> 
    <td ng-repeat="val in item2 | filer:blabla" 
     ng-if="shouldBeVisible(item2, $index, 'foto')">{{val}}</td> 
    </tr> 
</tbody> 

在控制器:

$scope.shouldBeVisible = (item, $index, prop)=>{ 
    return Object.keys(item)[$index] !== prop; 
} 
0

您可以使用$索引来访问val.field

例子:jsfiddle

HTML

<div ng-app="myApp"> 
    <ul ng-controller="MyCtrl"> 
    <li ng-repeat="item in items"> 
     {{item[0].field}} <!-- just a sanity check --> 
     <ul> 
     <li ng-repeat="val in item" ng-if="item[$index].field !== 'apple'">{{val.value}}</li> 
     </ul> 
    </li> 
    </ul> 
</div> 

代码

angular.module('myApp', []) 
    .controller('MyCtrl', function($scope){ 
    $scope.items = [ 
     [ 
     { 
     field: 'apple', 
     value: 'pie' 
     }, 
     { 
     field: 'chocolate', 
     value: 'cake' 
     } 
     ] 
    ]; 
    }); 
0

感谢,对anwsers,我解决了这个问题是这样的:

<tr ng-repeat="item2 in items2"> 
    <td ng-repeat="ite in item2" > 
     <img width="50px" ng-if="$index == 0" ng-src="{{ite}}" lazy-src/> 
     <span ng-if="$index != 0">{{ite}}</span> 
    </td> 
</tr> 

因为我越来越dinámically就这样从一个数据库中的数据:

app.controller("getItems2", function ($scope, $http, $rootScope) { 
var gi2 = this; 
$rootScope.$on("itemClicked",function(event,id){ 
    console.log(id); 
    gi2.getItems(id); 
}); 

this.getItems = function(idClase){ 
    $http.get('main/getItems2?idClase='+idClase). 
    then(function (response) { 
     $scope.columns = []; 
     response.data.forEach(function(e){ 
      e.Foto = "img/"+e.Foto; 
     }); 
     $scope.items2 = response.data; 
     console.log($scope.items2); 
    }, function (err) { 
     console.log(err); 
     // log error 
    }); 
}; 
this.getItems(1); 
}); 
相关问题