[英]aysnch:false takes long to load async true returns undefined angularjs ng-show
[英]ng-show if json returns [false] with angularjs
我是angulars的新手,我正在一个项目中,我希望div仅在json数据返回[false]
。 当查询没有结果时,有时我会得到json以返回[false]
。
JS
.controller('query_ctrl', function($scope, $http){ $http.get('http://localhost/db/templates/search/matches.php').success(function(data){
console.log(JSON.stringify(data));
$scope.matches=data;
});
})
的HTML
<div ng-controller="query_ctrl">
<div align="center" ng-show ="$scope.matches='false'">json returned false
</div>
<div align="center" ng-show ="$scope.matches!='false'">json returned true
</div>
</div>
您无需在HTML中引用$ scope。
<div ng-controller="query_ctrl">
<div align="center" ng-show ="matches=='false'">json returned false
</div>
<div align="center" ng-show ="matches!='false'">json returned true
</div>
</div>
另外,正如上面的评论所说-我不确定为什么要将值与字符串'false'进行比较,我假设您想将其与布尔值false进行比较。 见下文。
<div ng-controller="query_ctrl">
<div align="center" ng-show="matches==false">json returned false
</div>
<div align="center" ng-show ="matches!=false">json returned true
</div>
</div>
您可以通过将match作为表达式来使ng-show表达式更简洁,ng-show将评估它是否为真实值,请参见下文。
<div ng-controller="query_ctrl">
<!-- Will show if matches is a false value -->
<div align="center" ng-show="!matches">json returned false
</div>
<!-- Will show if matches is a truthy value -->
<div align="center" ng-show ="matches">json returned true
</div>
</div>
您在第一个ng-show中缺少一个=
并且也不需要引用$ scope,应为:
ng-show ="matches=='false'"
除了$ scope未在html文件中使用之外,其他一切似乎都很好。
<div ng-controller="query_ctrl">
<div align="center" ng-show ="matches=='false'">json returned false
</div>
<div align="center" ng-show ="matches!='false'">json returned true
</div>
</div>
仅将$ scope.matches替换为matchs。
“ ng-show”或“ ng-if”均可用于此目的。
您无需在ng-show附近定义比较操作,甚至可以执行以下操作:
<div align="center" ng-show ="$scope.matches"> // Boolean
.controller('query_ctrl', function($scope, $http){ $http.get('http://localhost/db/templates/search/matches.php').success(function(data){
console.log(JSON.stringify(data));
$scope.matches="data here";
}).error(function(error){
console.log("Error here");
$scope.matches="Error here"};
})
然后,您可以在HTML文件上编写:
<div ng-controller="query_ctrl">
<div align="center" ng-show ="matches=='data here'">json returned true
</div>
<div align="center" ng-show ="matches=='Error here'">json returned false
</div>
</div>
您要做的就是放宽html中的$ scope,这是因为将HTML页面视为范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.