How to use variable/function outside of the script
Question
Hello @artf , How can i access the variable as well functions which is outside of the script.
My code is like,
var temp = {};
function fnName() {
// Here is some script execution and will do it on global variables
}
bm.add("image-cropper",
{
label: '<div class="gjs-block-label">Image Cropper</div>',
tab: "2",
content: {
script: function () {
// But here i am getting temp is undefinded.
if (Object.keys(temp).length === 0) {
temp = {};
}
temp = {
a: "abcd",
b: "xyz"
}
// Here is the further code and using temp.
// As well i also need to use `fnName()` function in some cases.
},
content: '<div><h1>Hello</h1><img id="staticId" onclick="jsFunc(staticId)" src="imagePath" /><div>'
}
}
)
I am using angular.js(Angular1). And the above code is for the controller file. Can you please assist me how can i use this global variable as well function in my script?
Answers (2)
You can't, they're not in the same context, read carefully this page https://grapesjs.com/docs/modules/Components-js.html
You need to copy your global variables inside the script property or you can inject them in the canvas as a dependecy
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1733
How to remove script tag which is added by add block manager script
Hello @artf I want to export final/production based html content excluding the script(Only which is added by block manager). Suppose i am a...
Issue #1524
Howto add Blocks?
Hello all, it turns me crazy but I do not understand how to add blocks. I can write them myself following tutorial, but when I load plugin,...
Issue #980
[BUG] Related with task https://github.com/artf/grapesjs/issues/873
Hello, After restoring editor from db still have some part of bug present. It seems inside of script: function () traits will have old valu...
Issue #1836
How to add !important in all the generated apis
Hello @artf, While i am going to edit some html from editor with the help of css then i need to add !important into all the css which is ch...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.