angular.module('clientApp')
.controller('ProjectCtrl', function ($scope) {
this.items = [
{
"_id": "1",
"title": "The first scene",
"text": "And then something awesome happened..."
},
{
"_id": "2",
"title": "The second scene",
"text": "Oh shit..." }
];
})
.directive('myItemEditor', ['$timeout', function ($timeout) {
function link(scope, element, attrs) {
scope.id = scope.item._id;
scope.text = scope.item.text;
scope.title = scope.item.title;
var editorId = '#item-editor-'+scope.item._id;
var toolbarId = '#item-toolbar-'+scope.item._id;
$timeout(function() {
var editorElement = $(editorId)[0];
var toolbarElement = $(toolbarId)[0];
createEditors(editorElement, toolbarElement);
},0);
}
return {
restrict: 'E',
templateUrl: 'views/item-editor.html',
link:link
};
}]);
function createEditors(editorElem, toolbarElem) {
var quill = new Quill(editorElem);
quill.addModule('toolbar', { container: toolbarElem });
}
<div ng-repeat="item in project.items">
<my-item-editor></my-item-editor>
</div>
item-editor.html
<h2>{{::title}}</h2>
<!-- Create the toolbar container -->
<div class="quill-item-toolbar" ng-attr-id="item-toolbar-{{::id}}">
<button class="ql-bold">Bold</button>
<button class="ql-italic">Italic</button>
</div>
<!-- Create the editor container -->
<div class="quill-item-text" ng-attr-id="item-editor-{{::id}}" >
<div>{{::text}}</div>
</div>
--
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/d/optout.
Hi Kevin,
The way you put it up is Ok. The $timeout is not beautiful, but gets the job done. You need it, because, as you had noticed, angular works kind of async. As an alternative, you can take out the {{::text}}
out of your template, and use the Quill.setContents
in your link function to load the text into your editor.
Regards
Sander
Am I correct in assuming that the reason $timeout works is because it puts the closure at the end of the event loop queue so that all of the angular binding functions have fired before the $timeout function is executed?
--
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/x_BTRTKa2fo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Yeah, I'm surprised there isn't an "afterDigestLoop" event. Seems like something that would come up a lot.
<div class="quill-item-text" hook-up-quill ng-model='text'>
</div>
Stewart. Thanks for the detailed response. One thing, though."...or just be very straightforward and move the third-party initialization into the custom directive's link function.." -- I believe this is exactly what I tried and it didn't work because the bound expressions hadn't been resolved (or whatever the term is) yet.Yeah, I'm surprised there isn't an "afterDigestLoop" event. Seems like something that would come up a lot.Thanks,Andrew
Keep in mind too that the DOM will be changing every time the collection changes, so you will have to destroy/recreate stuff every time that happens.
Now you are adding ate least the row-count of extra watchers to your view.