Smart Tech for a better Web
Recently I wanted to write some kind of generic directive which loads some initial data into a specific scope. Something like:
Of course, this is not very generic and I thought it would be a good idea to use the powerful AngularJS expressions and create a JSON object. More like:
Now this approach would be fine, but the single quotes are not valid json. If you take this attribute value and perform angular.fromJson on it, it will fail miserable. In turn, AngularJS expression can handle this perfectly. With expressions it would look like:
The double-brackets on the beginning and the end make the inner json to an expression. So far so good. Let's look at a first simple example how to evaluate that.
This is my link:
I want it to open a modal (Twitter Bootstrap, to be specific). Basically my goal is to initialize my modal with some values, which are delivered as JSON and by the my-show-modal-data attribute. I even tell my directive which modal to open (#test), which is where the magic happens.
Basically I am creating a simple directive here with defining a "link" function. The directive is on myShowModal of course, as it does only make sense to set data when a modal is defined. The interesting part is inside the openDialog function. I can access my data from the attrs argument, right with the name. Its a String (you can test it with angular.isString()) which comes in the right format (by magic). The whole beast is executed when somebody clicks on it.
Now the execution happens after the everything is in place. In the following use case things are not so easy.
What if we would leave out the click handler? Then we have a problem. Imagine you would like to load some data into your controller, without any action. Like that:
Now let's make a simple directive:
It should first output all my attributes, then the tab-load value. Which should be a String. But I were running into trouble:
I marked 3 interesting places of my Chrome Debugger for you. First, you see the summary output of attrs. tabLoad is undefined. If you open the object, you'll see the expected value. But when you access it in your code, you get undefined. After programming use case 1, this behavior is not really what one would expect. It kept me thinking for a while.
I explain it like that. When we link the directive, the expression is not executed, while "static values" are known. This is why we get undefined in the summary and undefined from the second console.log. When we open the object tree in the browser, the expression has been executed in the meantime and thus you can see it.
Once I had that theory in mind, I could see the difference to use case 1: the event handler is executed after the expression has been executed. That way it can access the JSON.
Still I wanted some generic function!
Reading in the directive docs I found the relevant information:
Use $observe to observe the value changes of attributes that contain interpolation (e.g. src=""). Not only is this very efficient but it's also the only way to easily get the actual value because during the linking phase the interpolation hasn't been evaluated yet and so the value is at this time set to undefined.
Now thing became to clear up. You can "observe" the attribute value. Once the expression is executed, the observer will fire and you'll get want you want:
This is not very elegant, but have no idea how to do it otherwise.