what is the best time to get a div that has a dynamic id.

40 views
Skip to first unread message

Johan Compagner

unread,
Aug 22, 2014, 8:31:37 AM8/22/14
to ang...@googlegroups.com
Hi,

lets say i want to wrap this: http://justgage.com/ in a angular component
so i create a nice directive:

.directive('JustGage', function() {
return {
restrict: 'E',
scope: {
},
controller: function($scope, $element, $attrs) {
$scope.markupId = //something dynamic, uniek on the page.could also be given into the scope or attributes.
},
link: function(scope, element, attrs) {
console.log("childid: "  + element[0].children[0].id)


scope.gauge = new JustGage({
   id: element[0].id, 
   value: scope.model.value, 
   min: scope.model.min,
   max: scope.model.max,
   title: scope.model.title,
label: scope.model.label,
relativeGaugeSize: true,
       donut: scope.model.donut
});
},
template: '<div id="{{markupId}}"></div>'

};
})

that childid will print: {{markupId}}

because i need to wait even 1 more digest cycle before i can do new JustGage (because only then that element has the right id)

How to fix this nicely? If you include 3th party libs that want to bind on an id. And if that id is dynamic what is the event/time to really know, ok now it is done, now you can do what you want.

johan


Johan Compagner

unread,
Aug 22, 2014, 9:59:11 AM8/22/14
to ang...@googlegroups.com
the thing i did come up with for now is using it with a dynamic template:

element.html("<div id='" + scope.model.markupId + "'></div>").show();

       $compile(element.contents())(scope);
       
scope.api.updateGauge = function(value, max){
scope.gauge.refresh(value, max)
}
console.log("childid: "  + element[0].children[0].id)
scope.gauge = new JustGage({
   id: element[0].children[0].id, 
   value: scope.model.value, 
   min: scope.model.min,
   max: scope.model.max,
   title: scope.model.title,
label: scope.model.label,
relativeGaugeSize: true,
       donut: scope.model.donut
}); 

then it works, is that the right/only way to do it?



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/CoSQZIwO2Os/unsubscribe.
To unsubscribe from this group and all its topics, 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/d/optout.



--
Johan Compagner
Servoy
Reply all
Reply to author
Forward
0 new messages