2016-08-24 65 views
1

我想基于当前时间显示商店是打开还是关闭的状态。所以这里是代码。如何使用if语句以角度js为条件

在控制器

$http.get(httpUrl + 'shops/get_shops') 
.then(function (returns) { 
    $scope.shops = returns.data; 
}); 
var now = new Date(); 
$scope.current_time = now.getHours() + ':' + now.getMinutes() + ':00';// to get hh:mm:ss format 

在视图

<div ng-repeat="item in shops"> 
    <span ng-if="item.open_hour != '00:00:00' && item.open_hour <= '{{current_time}}' && item.close_hour >= {{current_time}}">We're open</span> 
</div> 

的问题是,其结果是既满足了第一条件item.open_hour != '00:00:00'和其余的违背。因此,所有open_hour不是'00:00:00'的数据都会打开,无论open_hour和close_hour。

我是新来的angularjs,不知道如何做这样的事情。请任何帮助,将不胜感激。

+0

'<跨度NG-如果= “{{item.open_hour = '00:00:00' && item.open_hour <= '的current_time' && item.close_hour> =的current_time}}”>我们”重新开放'改为这个 –

+0

@HardikVaghani你的回答是错误的。 'item.open_hour <='current_time''→current_time它是一个范围变量不是一个字符串。 – gianlucatursi

+0

@HardikVaghani这是工作!但我认为它应该在current_time中没有引用。无论如何感谢 – Abaij

回答

3

当你在ng-if你不需要{{}}获得范围变种。

<span ng-if="item.open_hour != '00:00:00' && item.open_hour <= current_time && item.close_hour >= current_time">We're open</span> 
+0

是的你是对的。谢谢你让我知道 – Abaij