Javascript AngularJS ng在某个值为真时显示
我想显示一个符号(使用Javascript AngularJS ng在某个值为真时显示,javascript,angularjs,Javascript,Angularjs,我想显示一个符号(使用 Object.keys(filter.producers).length; <span ng-show="Object.keys(filter.producers).length != 0">you can use the JS Array.prototype.some function in your controller as follows: $scope.flag = Object.keys($scope.filter.producers).som
Object.keys(filter.producers).length;
<span ng-show="Object.keys(filter.producers).length != 0">you can use the JS Array.prototype.some
function in your controller as follows:
$scope.flag = Object.keys($scope.filter.producers).some(
function(el) {
return $scope.filter.producers[el];
});
Object.keys(filter.producers).length;
您可以使用JSArray.prototype。控制器中的一些函数如下所示:
<span ng-show="!flag">X</span>
<span ng-show="flag">you need another function to check all properties are true
or false
initially and when click (X)
link to set false
for a specific property and do not need two ng-repeat
in your DOM. you can try my solution.
like:
your controller:
angular.module('myApp', [])
.controller('Ctrl', function($scope) {
$scope.ctrlTest = "Filters";
$scope.filter = {
"producers": {
"Ford": true,
"Honda": true,
"Ferrari": true
}
};
$scope.hideMe = function(key) {
$scope.filter.producers[key]=false;
$scope.checkAllproperty(); // call again to check all property
};
$scope.checkAllproperty = function() {
var allTrue = false;
for(var key in $scope.filter.producers) {
if($scope.filter.producers[key] === true){
allTrue = true;
}
}
$scope.allTrue = allTrue;
};
$scope.checkAllproperty(); // initial call when loaded
});
在HTML中使用以下标志:
<span ng-show="!flag">X</span>
<span ng-show="flag">you need another function to check all properties are true
or false
initially and when click (X)
link to set false
for a specific property and do not need two ng-repeat
in your DOM. you can try my solution.
like:
your controller:
angular.module('myApp', [])
.controller('Ctrl', function($scope) {
$scope.ctrlTest = "Filters";
$scope.filter = {
"producers": {
"Ford": true,
"Honda": true,
"Ferrari": true
}
};
$scope.hideMe = function(key) {
$scope.filter.producers[key]=false;
$scope.checkAllproperty(); // call again to check all property
};
$scope.checkAllproperty = function() {
var allTrue = false;
for(var key in $scope.filter.producers) {
if($scope.filter.producers[key] === true){
allTrue = true;
}
}
$scope.allTrue = allTrue;
};
$scope.checkAllproperty(); // initial call when loaded
});
X
您需要另一个函数来检查所有属性最初是否为true
或false
,当单击(X)
链接为特定属性设置false
,并且在DOM中不需要两个ng repeat
。您可以尝试我的解决方案
比如:
您的控制器:
在html中:调用hideMe
函数设置false
{{ctrlTest}}
X
在您的情况下,filter.producers的值是以下对象: {“Ford”:真的,…}。它肯定不等于真的或假的!如果我没弄错你的问题,你应该做的是提取所有它的值,并检查这些值中是否有一个是真的或假的,类似这样的:
//在控制器中
$scope.anyProducerTrue=函数(){
var anyTrue=false;
angular.forEach($scope.filter.producers,函数(值,键){
如果(true==值)anyTrue=true;
});
返回anyTrue;
}
//在你看来
X
问题不清楚。请提供更详细的解释。您忘记问这个问题了!在您的JSBin中尝试了解决方案,但当所有值都设置为FALSE时,它不会更改符号。当范围($filter.producers)中的一个或多个值时,问题正文的这一行的意思是什么?
。现在你说的是另一个场景,如果fx$scope.filter={“生产者”:{“福特”:false,“本田”:false,“法拉利”:true}}我想要但是如果$scope.filter={“生产者”:{“福特”:false,“本田”:false,“法拉利”:false}我希望X-to-appearI尝试了您的解决方案,但当所有值都为false时,它似乎不会更改图标,请检查此更新的JSFIDLE。在将all更改为false后,您需要单击“run”按钮。这是相同的代码,所有值都设置为false:我只需在手动将作用域的所有值设置为false时使其工作,因此t与我的问题略有不同,但还是要感谢你的方法。它工作完美,而且是更干净的解决方案。我会将其标记为有效。谢谢!