1

I have created a directive in angular that has an user-id as attribute.

<my-directive user-id="123"></my-directive>

I want to use it in my profile page, but i get that id in my controller after an http call. So i have tried to use it like that:

<my-directive user-id="user.id"></my-directive>

But It loads before the user.id gets the value so its undefined. I should wait until the binding is done. I could watch user but like this it loads twice the directive. I hope i explained myself

2
  • Can you show the directive code? Commented Aug 4, 2015 at 10:05
  • You could also use ng-if on the directive. Commented Aug 4, 2015 at 10:07

1 Answer 1

2

Use ng-if to only render the directive when user.id is defined:

<my-directive user-id="user.id" ng-if="user.id >= 0"></my-directive>
Sign up to request clarification or add additional context in comments.

1 Comment

Isn't there a better way? Then every time you use the directive, you'll have to think about the ng-if. Isn't there a flag provided by angular telling it to wait? The only solution I could come up with is to wrap the directive in another directive which does the ng-if for you, so the enduser of the directive won't have to think about the ng-if anymore...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.