javascript - AngularJS ngOptions filter array by boolean field -
javascript - AngularJS ngOptions filter array by boolean field -
i have array:
this.chapters = { 1: { name: 'chapter 1', show: false }, 2: { name: 'chapter 2', show: true }, 3: { name: 'chapter 3', show: true } };
and want show chapters show value true in select
<select ng-model="cart.newchapter" ng-options="v.name (k, v) in cart.chapters"></select>
now it's showing chapters, need filter them. i've been trying apply filter didn't work. can help me? oh! can't alter boolean type of.
you cannot apply filters objects, arrays. so, have 2 options here, alter chapters
array or pre-filter chapters
object using function this:
$scope.filter = function(chapters) { var result = {}; angular.foreach(chapters, function(chapter, key) { if (!chapter.show) { return; } result[key] = chapter; }); homecoming result; };
and on html:
<select ng-model="newchapter" ng-options="v.name (k, v) in filter(chapters)"></select>
class="snippet-code-js lang-js prettyprint-override">angular.module('app', []) .controller('ctrl', function($scope) { $scope.chapters = { 1: { name: 'chapter 1', show: false }, 2: { name: 'chapter 2', show: true }, 3: { name: 'chapter 3', show: true } }; $scope.filter = function(chapters) { var result = {}; angular.foreach(chapters, function(chapter, key) { if (!chapter.show) { return; } result[key] = chapter; }); homecoming result; }; });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <select ng-model="newchapter" ng-options="v.name (k, v) in filter(chapters)"></select> </div>
javascript arrays angularjs ng-options
Comments
Post a Comment