javascript - How to use the AngularJS ngShow directive to show only if an element is in an array? -
my goal to, rapidly, have tag displaying authors associated given publication.
everything works when $scope.publications.authors
set single value. not work when multiple values stored in array, shown below.
i know conditional statement angularjs ngshow directive isn't proper 1 use arrays, can't seem figure out should use.
html:
<div ng-repeat="person in persons" ng-show="person.firstname == publication.authors">{{person.firstname}}</div>
js:
$scope.publications = [ {"title": "research paper", "authors": ["bill", "george"]}; $scope.persons = [ {"firstname": "bill", "lastname": "smith"}, {"firstname": "george", "lastname": "jones"}, {"firstname": "mike", "lastname": "thomas"};
according data structure of publications
, need nested repeater. , can use function current person
, publication
passed in drive display.
<div ng-repeat="publication in publications">{{publication.title}} <br/> <div ng-repeat="person in people" ng-show="show(person, publication)">{{person.firstname}}</div> </div> function ctrl($scope) { $scope.publications = [{ "title": "research paper", "authors": ["bill", "george"] }]; $scope.people = [{ "firstname": "bill", "lastname": "smith" }, { "firstname": "george", "lastname": "jones" }, { "firstname": "mike", "lastname": "thomas" }]; $scope.show = function (person, publication) { return publication.authors.indexof(person.firstname) >= 0; } }
Comments
Post a Comment