Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ng-show with promise expression

I'm using ng-show with an expression that resolves to a promise that resolves to a boolean. When I do this I get the 10 digest iterations overflow.

See http://plnkr.co/edit/XibYM0kCnXhKjNUeTsp3?p=preview

  <body ng-controller="MainCtrl">
    <p ng-show="returnsABoolean()">non promise</p>
    <p ng-show="returnsAPromiseThatResolvesToABoolean()">promise</p>
  </body>

Ctrl:

  $scope.returnsABoolean = ()->
    true

  $scope.returnsAPromiseThatResolvesToABoolean = ()->
    $q.when(false)

I know that {{somePromise}} will resolve, but {{returnsAPromiseThatResolvesToABoolean()}} seems to cause the same issue.

Any ideas? I'd expect this to work..

like image 611
Roy Truelove Avatar asked Sep 06 '13 21:09

Roy Truelove


1 Answers

AngularJS resolves the promise for template binding automatically. However, you should use the promise in ng-init to prevent the digest cycle from returning a new promise every tick.

<p ng-init="v=returnsAPromiseThatResolvesToABoolean()" ng-show="v">promise</p>
like image 57
zs2020 Avatar answered Sep 28 '22 05:09

zs2020