I have a div
holding some chat history. I would like for the div
content to scroll when it becomes full. I have this working using jQuery in another project, but what would be the proper AngularDart way to achieve this?
Simplifying a bit, in my HTML I have
<div class="chatHistory">{{ctrl.text}}</div>
(using style white-space:pre
) and in my controller I have the method
void addToChatHistory(String msg) {
text += msg;
var elt = querySelector(".chatHistory");
elt.scrollTop = elt.scrollHeight;
}
Issues:
- This code scrolls the
div
content but not quite enough because it sets thescrollTop
too quickly; i.e., even before the.chatHistory
can be updated by Angular. - Besides this (partial) solution doesn't feel very Angular in style given the use of
querySelector
.
I tried setting up a watch
on the text
field but that also fires too early. Suggestions?
For the first issue you can use Timer.run to defer the scroll execution :
For the second issue you can inject the Element managed by your controller and use
querySelector
on it :EDIT
I published a package containing this directive: http://pub.dartlang.org/packages/bwu_angular
-------
I would create a Directive/Decorator like
NgEventDirective
but for the resize event and add it to your div. In the event handler you set yourscrollTop
property.I found contradictory info about the resize event.
Onresize for div elements? is a workaround for browsers that don't support this event.
another page covering this topic: http://marcj.github.io/css-element-queries/
I tried to create an Angular implementation for the 'workaround' (2nd link) but run into this issue https://code.google.com/p/dart/issues/detail?id=18062 which contains info about a workaround but I didn't yet find time to implement it.
EDIT
I checked my attempt and it worked in Dartium with the version I downloaded today (Dart VM version: 1.4.0-dev.4.0 (Thu May 1 04:06:09 2014) on "linux_x64"). I haven't tried in other version since I created the issue.
(The code should be improved to make the directive more generic - the event method should be assignable by an attribute not hardcoded)
index.html
index.dart
If you're using an AngularDart component you can inject the shadowdom then select an element inside it to focus on. Didnt find a way of getting the container of the shadowdom.
ShadowDom.host might work, though its currently marked as experimental.