Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度1.6-如果数组中的元素是另一个对象中的键,则为ng_Javascript_Angularjs - Fatal编程技术网

Javascript 角度1.6-如果数组中的元素是另一个对象中的键,则为ng

Javascript 角度1.6-如果数组中的元素是另一个对象中的键,则为ng,javascript,angularjs,Javascript,Angularjs,在Angular中,我有这样一个数组: $scope.items =["blue","red","pink","yellow"]; 和另一个对象(我用于过滤) 我想做一个ng if,这样 <ul> <li ng-repeat="n in items" ng-if="n in filter.color"> </li> </ul> ng repeat将只显示$scope.items中存在于$scope.filter 因此,

在Angular中,我有这样一个数组:

$scope.items =["blue","red","pink","yellow"];
和另一个对象(我用于过滤)

我想做一个
ng if
,这样

<ul>
    <li ng-repeat="n in items" ng-if="n in filter.color">
    </li>
</ul>
ng repeat将只显示
$scope.items
中存在于
$scope.filter

因此,换句话说,它将只显示蓝色粉色


提前谢谢

您需要一个自定义筛选函数:

<ul>
    <li ng-repeat="n in items | filter: yourFilter">
    </li>
</ul>

$scope.filter={"color":{"blue":true, "pink":true},"shape":{"square":true}};

$scope.yourFilter = function(item) {
  //will be true when color has a property with the color and it's true
  return $scope.filter.color[item];
}
$scope.filter={“color”:{“blue”:true,“pink”:true},“shape”:{“square”:true}; $scope.yourFilter=函数(项){ //当“颜色”具有“颜色”属性且该属性为真时,将为真 返回$scope.filter.color[item]; }
您需要一个自定义筛选功能:

<ul>
    <li ng-repeat="n in items | filter: yourFilter">
    </li>
</ul>

$scope.filter={"color":{"blue":true, "pink":true},"shape":{"square":true}};

$scope.yourFilter = function(item) {
  //will be true when color has a property with the color and it's true
  return $scope.filter.color[item];
}
$scope.filter={“color”:{“blue”:true,“pink”:true},“shape”:{“square”:true}; $scope.yourFilter=函数(项){ //当“颜色”具有“颜色”属性且该属性为真时,将为真 返回$scope.filter.color[项目]; }
由于
$scope.items
是一个数组,
$scope.filter
是一个对象,因此需要一个函数来测试值:

angular.module('test',[]).controller('testController',function($scope)
{
$scope.items=[“蓝色”、“红色”、“粉色”、“黄色”];
$scope.filter={“color”:{“blue”:true,“pink”:true};
$scope.checkColor=函数(值、索引、数组)
{
返回$scope.filter.color[value];
};
});

  • {{n}

由于
$scope.items
是一个数组,
$scope.filter
是一个对象,因此需要一个函数来测试值:

angular.module('test',[]).controller('testController',function($scope)
{
$scope.items=[“蓝色”、“红色”、“粉色”、“黄色”];
$scope.filter={“color”:{“blue”:true,“pink”:true};
$scope.checkColor=函数(值、索引、数组)
{
返回$scope.filter.color[value];
};
});

  • {{n}

你能发布更多的
$scope.filter
的内容吗?你只有一种颜色,如果它们是两种,会是什么样子?编辑文章以在过滤器上显示两种颜色:)你能发布更多
$scope.filter
的内容吗?你只有一种颜色,如果它们是两种,那会是什么样子?编辑这篇文章,在滤镜上显示两种颜色:)我会把这标记为正确的,即使考虑到角度开发,没有ES6表达式更安全(因为SEO原因-它破坏了谷歌爬虫),所以@sthames42回复和今天一样安全)我公司的所有角度应用都是私有的,所以我有这个不良的SEO习惯。我会编辑答案我会把这标记为正确的,即使考虑到Angular development不使用ES6表达式也更安全(因为SEO原因-它破坏了google爬虫),所以@sthames42回复与今天一样安全)我公司的所有Angular应用程序都是私有的,所以我有这种不好的SEO习惯。我将编辑答案