2016-04-27 98 views
1

我想每10秒进行一次http调用,然后显示一个div,如果rootScope.mailSent等于true。通话很好。

在我的控制器中我已经做到了:

$interval(function(){ 
     $http.get("/api/sentMails") 
     .then(function(response) { 
      if (response.data.length>0){ 
       $rootScope.mailSent = true; 
      } 
     }); 
    }.bind($rootScope), 10000); 

笔者认为:

<div ng-show="{{mailSent === true}}" class="pastille" ng-cloak=""></div> 

但DIV从来没有显示,而mailSent值为true。 我错过了什么吗?

+0

你不需要'{{}}''中的NG-show'因为它接受一个表达式。 –

+0

是否每次请求完成时都尝试console.log($ rootScope.mailSent)? –

+0

是的@MuliYulzary是正确的,你不需要''{}}'在'ng显示' –

回答

2

语法不正确,试试这个:

<div ng-show="mailSent" class="pastille" ng-cloak=""></div> 
1

试试这个

<div ng-show="mailSent" class="pastille" ng-cloak=""></div> 

NG-展会将于true或false值。如果发送的邮件已设置“$ rootScope.mailSent = true;”。所以ng-show会得到真正的价值。不需要像这样做比较。

希望这会为你工作。

0

你不需要{{}}与NG-显示试试这个:

<div ng-show="mailSent === true" class="pastille" ng-cloak=""></div> 
0

直接使用在$ rootScope场可能会引起很多麻烦。例如,如果我将他们的解决方案包装在另一个具有ng-if指令的div中,所有解决方案都不起作用。

为了确保不会发生使用中介字段和不要忘记来初始化它。由于它位于rootScope中,因此将其初始化为angular.run块。

最后:ng-show将表达式作为参数no插值(括号)。

<div ng-show="mail.sent == true" class="pastille"></div> 
$rootScope.mail = {sent:false};// initialize this in the controller pass it to true when you have the condition met 
+0

为什么比较当它已经是一个布尔值? –

+0

只是一些东西我也使用角度和形式有一个失败快,当我得到真或假的字符串,而不是布尔值:) – Walfrat

0

正如前面的答案指出的那样,它似乎是一个范围问题。如果您将$log附加到您的范围,您可以在模板中调用它。这里有一个漂亮的窍门:

<button data-ng-click="$log.log(put your model variable here)"></button> 

如果您添加到您的模板,那么你可以点击它,你会在什么是变量里面的控制台中看到日志。

如果您不使用controllerAs语法,我强烈建议您避免使用可变阴影。您可以在$routeProvider代码中或在声明控制器时在模板中执行此操作。

然后参考是scopeName.mailSent

然后您的按钮应该是这样的:

<button data-ng-click="scopeName.$log.log(scopeName.mailSent)"></button>