--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
Hi Olivier,
I will go over your reply and answer inline.
The reason for the transclude is the replaceWith of the base element of the directive. The directive replace the custom element <dropdown> with valid markup.
I thought that was pretty the point of angularjs, i.e the ability to extend html.
Euhm, yes, one of the points is extending HTML. However, what’s the use of replacing HTML with so called ‘valid HTML’? Who is validating? And when? Are you going to run a HTML validator on your HTML after Angular is done with it?
If you want, you can write HTML that can be validated, but it takes extra effort. Personally I don’t see any added value there.
I dont know how to split that in 2, I never made directives that communicate with each other, and also I did not get to read about it.
Well, that’s not a excuse is it? ;)
at this stage the purpose of this directive is rather simple. It is a replacement for the html element <select>. The only additional feature compared to a <select> is that it can have html with the <option> element instead of text. Later I will add more features, like a search field.So now, I think the logic in it can be simply described. There is 2 parts :
1. translate the custom markup to valid markup.
2. handle interactivity (show/hide of the options and click-to-select)
I guess the stuff that feels weird to you are rather located in the part 1:
a.The attributesToString() function is used to copy the attributes of the original element <dropdown> to the replacement element, a <span>. I did not knew any angularjs prescription about this, but I thought it would be nice if the directive let developer that use it add their attributes if they need to, for whatever reasons.
b.
The angular.forEach loop copies the <option> contents and values to a <li> collection and sets the default value in the top <span>.
c.
Stuff mades in steps (a) and (b) are populated into the html structure, and then compiled with $compile to give angular knowledge of the event handlers in it.
part 2:
just 2 methods show() and select() to respectively show the list and select an item in it.
What are the parts that appear to you as fragile ?
Well, where to start? You replaced dom manipulation with string manipulation. Although that is possible as you have shown, I don’t think it’s such a good idea.
Then you are using the DOM/HTML/Template as source where your data is coming from. Again possible, but it defeats the whole idea of angular!
Also you are trying to make the final result in the DOM is validatable HTML? Seems backward to me, and also if this is a demand, it’s failing at that too!
<ul ng-show="display" class="ng-scope ng-hide">`
<li ng-click="select($event);" data-value="1"> <span class="bla">abc</span><span class="bli">def</span></li>
<li ng-click="select($event);" data-value="2">xyz</li>
<li ng-click="select($event);" data-value="3">foo</li>
<li ng-click="select($event);" data-value="4">bar</li>
</ul>
That is what your code is producing as final result. Looks nice, but all the ng-xxx aren’t valid HTML. This is easily solved, but that’s not the point!
Let me know what your thoughts are now, and we will see how to proceed.
Regards
Sander
Olivier,I gave it a shot: http://plnkr.co/edit/FZrpU4?p=preview
--
--
Olivier,
Put it in a array on the scope, and iterate.
in the controller:
$scope.items = [
{value : 1 , description : 'Hello darkness,'},
{value : 2 , description : 'my old friend'},
{value : 3 , description : 'I'v come to talk with you again.'},
{value : 4 , description : 'Because a vision softly creeping'},
];
in template: <doptions ng-repeat='item in items' value='item.value>{{item.description}}</doptions>
Regards
Sander