这可能是关于Angular的一个有趣的问题。但这是完整的背景。
我使用Angular插件TextAngular(1.2.2)。我试图用额外的按钮扩展工具栏。在代码(客户端)中扩展工具栏的效果相当不错(下面的se片段)。
但我想创建我定义服务器端的按钮 - 因此必须将它们下载到客户端。当我引入异步调用(通过使用服务)并尝试在回调中注入配置时,工具栏按钮不会显示。我怀疑这是因为角度引擎需要在实例化之前配置textangular插件。我试图创建一个提供程序,而不是服务,并将其提供给.config(),但后来我得到一个关于提供程序未找到的例外。
静态方法效果很好。但是我应该如何用动态数据来解决这个问题?
//Module
var myApp = angular.module('myApp', ['textAngular']);
//Configuration
myApp.config(function($provide) {
$provide.decorator('taOptions', ['taRegisterTool', '$delegate',
function(taRegisterTool, taOptions) {
//I found these helpers somewhere
var insertToolHelper = {
insertTextAtCursor: function(text) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(text));
}
} else if (document.selection && document.selection.createRange) {
document.selection.createRange().text = text;
}
},
moveCaret: function(charCount) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount > 0) {
var textNode = sel.focusNode;
sel.collapse(textNode.nextSibling, charCount);
}
} else if ((sel = window.document.selection)) {
if (sel.type != "Control") {
range = sel.createRange();
range.move("character", charCount);
range.select();
}
}
}
};
var customToolbarElements = [{
value: "$name",
description: "Name",
faIcon: "fa fa-user"
}, {
value: "$today",
description: "Date",
faIcon: "fa fa-calendar"
}];
taOptions.toolbar.push([]); //Tilføj ny toolbar
angular.forEach(customToolbarElements, function(item) {
taRegisterTool(item.description, {
iconclass: item.faIcon,
tooltiptext: item.description,
action: function() {
insertToolHelper.insertTextAtCursor(item.value);
return insertToolHelper.moveCaret(1);
}
});
// register the tool with textAngular
taOptions.toolbar[4].push(item.description);
}, this);
return taOptions;
}
]);
});
基于Simeons的帖子,我设法获得了这样的动态工具按钮:
//Module
var myApp = angular.module('myApp', ['textAngular']);
//Service
myApp.service('ConfigurationData', [
'$http', '$q', function (http, q) {
var deferredConfig = q.defer();
//This method returns someting like [{ value: "name", description: "Person name", faIcon: "fa fa-user" }];
http.get(location.pathname + '/api/templates').success(function (data) {
return deferredConfig.resolve(data);
});
return {
getConfig: function () {
return deferredConfig.promise;
}
};
}
]);
//Controller
myApp.controller('SettingsCtrl', [
'$scope', 'textAngularManager', 'ConfigurationData',
function ($scope, $rootScope, textAngularManager, configurationData) {
var insertToolHelper = {
insertTextAtCursor: function (text) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(text));
}
} else if (document.selection && document.selection.createRange) {
document.selection.createRange().text = text;
}
},
moveCaret: function (charCount) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount > 0) {
var textNode = sel.focusNode;
sel.collapse(textNode.nextSibling, charCount);
}
} else if ((sel = window.document.selection)) {
if (sel.type != "Control") {
range = sel.createRange();
range.move("character", charCount);
range.select();
}
}
}
};
configurationData.getConfig().then(function (config) {
var customToolbarElements = config.jsonArrayFromService;
angular.forEach(customToolbarElements, function (item) {
var desc = item.description ? item.description : "unknown tool";
textAngularManager.addTool(desc, {
iconclass: item.faIcon ? item.faIcon : "fa fa-gear",
tooltiptext: desc,
action: function () {
insertToolHelper.insertTextAtCursor(item.value);
return insertToolHelper.moveCaret(1);
}
});
}, this);
});
}
]);
答案 0 :(得分:2)
您想查看textAngularManager服务,您要查看的两个函数是:
// toolkey, toolDefinition are required. If group is not specified will pick the last group, if index isnt defined will append to group
textAngularManager.addTool(toolKey, toolDefinition, group, index)
// adds a Tool but only to one toolbar not all
textAngularManager.addToolToToolbar(toolKey, toolDefinition, toolbarKey, group, index)
它们不需要在提供程序中调用,因此您可以随时添加工具(还有一个removeTool函数)。
您遇到的问题是config
仅在加载模块时调用一次。