1

I've got a condition in ng-if="ctrl.zoo" (in some directive where ctrl.zoo = ctrl.foo && ctrl.bar). The problem is that bar initializes by async function call and angularJS for some reason doesn't update my UI even though I included bar to my scope. I google that it's a bad idea to watch other variables to assign a new value for another (basically watch for changes of bar). It did work perfectly though if I inline ctrl.zoo (e.g.ng-if="ctrl.foo && ctrl.bar"`). Is there any simple fix about it?

1 Answer 1

4

If you assign zoo = foo && bar in your controller, that is the value zoo is going to stay as regardless of whether or not the value of bar changes.

If you want zoo to update, you have a couple of options:

  1. Update the value of zoo again to foo && bar after your async function call is finished.

  2. Write a function in your directive controller called getZoo() which returns foo && bar. Use that function in your directive's template and when you update the value of bar on the directive's scope, the scope will run a digest loop, and that function will return the new value. The reason that inlining ctrl.foo && ctrl.bar in your template works is because you're doing essentially this.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I tried the 1st way: and it doesn't work as well, now I'll try the second one. I had 1: this.zoo = false; 2: async(result) -> {this.foo = result; this.zoo = this.foo && this.bar; }
Were you using an async function? If so, it may have been failing because that would not be tied into Angular's digest loop. You would need to run $rootScope.$digest() or $timeout() or something similar to force a digest loop to run. It would be much cleaner to avoid this and use Angular's $http service to perform HTTP request instead, as this is tied into the digest loop and will therefore update your view with the new data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.